Command sent on Bot stop/locking - php

I'd like to know if a command is sent from telegram when a bot is stopped/locked and what is. I've read the telegram bot documentation and googled for this problem but I haven't find a solution.
Where could I find?

Every time you send a request to Telegram servers you receive a response.
The response contains a JSON object, which always has a Boolean field ok and may have an optional String field description with a human-readable description of the result. If ok equals true, the request was successful and the result of the query can be found in the result field. In case of an unsuccessful request, ok equals false and the error is explained in the description. An Integer ‘error_code’ field is also returned, but its contents are subject to change in the future.
From the official documentation.
As example when you make a request to a blocked bot you may receive a response like that.
{
"ok":false,
"error_code":403,
"description":"[Error]: Forbidden: can't write to private chat with deleted user"
}
So when the field ok is false you know something went wrong.

You can check for my_chat_member update. When user is blocking your bot from his chat (it may be a converation or just a private chat), you will receive it with those fields.
{
chat: {
id: 1,
username: "...",
type: "private",
},
from: {...},
old_chat_member: {status: "member"}, // is your bot status
new_chat_member: {status: "kicked"}, // is you new bot status
}

Related

Woocommerce webhook response 200 message The Rest API is unavailable

When woocommerce (webhook) send a success message (200) but the body contain the below
{
"errors": {
"rest_api_unavailable": [
"The Rest API is unavailable."
]
},
"error_data": []
}
Has anyone happened this error?
Thanks in advance
I did some additional testing on the site I was experiencing this on and narrowed it down to define('ALTERNATE_WP_CRON', true) being set in wp-config.php. If you have this set, try commenting it out. In my tests, the correct payload was sent every time after commenting this constant out. Also, on a completely different site that I configured the same exact webhook/endpoint on that had no issues, as soon as I set ALTERNATE_WP_CRON to true, it started sending rest_api_unavailable for the payload. Please refer to these links for additional information and bug report:
https://github.com/woocommerce/woocommerce/pull/26878
https://github.com/woocommerce/woocommerce/issues/28363

How to use ForceReply in a telegram bot

I'm developing a telegram bot using php and a web hook. All it's fine but sometimes I would like to "wait for a reply" from the user. For example:
If the client write /info without any parameters I would like to show a "usage" message and ask&wait for a ID parameter.
I know there is a property "ForceReply" to force for a reply, but when I set it up nothing happens, and I don't know how to know if the client message its a reply for my question.
Do I have to put my php server on hold? (I think it would be a bad practice) Do I have to whait for a type of message?
Thanks
When you use getUpdates or receive updates via a webhook, the update message will contain a field like reply_to_message field. You can use this to compare it to the message you sent.
If you are running your script via webhooks I would assume that it only executes when it receives a message. If so, I would suggest you use something like memcache/redis to store the message you are expecting a reply for and then when the reply comes, you can compare it to the value stored:
<?php
// This script triggers as a webhook
$message = file_get_contents('php://input');
$message = json_decode($message, true);
$cache = new RedisClient('localhost');
if ($message->reply_to_message == $cache->get('original.message.id'))
{
var_dump('message reply received');
}
The example above is some "pseudo" code that you can use in a webhook to check for a reply to a specific message.

ZenDesk API result does not match call when deleting

I am using the ZenDesk API (https://developer.zendesk.com/rest_api/docs/core/introduction) to synchronise a ZenDesk setup with another client database. When I try to delete an organization, I get a response that seems to suggest an update call has been made.
According to the documentation (https://developer.zendesk.com/rest_api/docs/core/organizations#delete-organization) the call should be DELETE /api/v2/organizations/{id}.json where the {id} is the id of the organization.
I have written code that I believe to be correct, and checked this with Fiddler. The call comes through on Fiddler as:
DELETE /api/v2/organizations/39005971.json HTTP/1.1
The raw request view shows (with redactions):
DELETE https://<redacted>.zendesk.com/api/v2/organizations/39005971.json HTTP/1.1
Authorization: Basic <redacted>
Host: <redacted>.zendesk.com
Accept: */*
Content-Type: application/json
and the response comes back as:
{
"error":"RecordInvalid",
"description":"Record validation errors",
"details":{
"name":[
{
"description":"Name: has already been taken",
"error":"DuplicateValue"
}
]
}
}
This is the same response that is given if you try to insert an organization with the same name as an existing one. From the documentation, the basic difference between deleting and updating a record is that delete requests use DELETE and updates use PUT - the endpoint URL is the same.
Does anyone have any suggestions? I can provide upstream code (in PHP) if needed, however as Fiddler is picking up the request as a correctly formatted DELETE, I'm not sure that the code is going to help.
I actually work for Zendesk and figured this out personally. You seemed to have run into a bug having to do with the max characters an organization name can have. You probably had a couple organizations whose names were more than 255 characters long and after getting truncated to 255 were the same name. Now validation issues are popping up. I'm really sorry about that!
I would send a request to https://support.zendesk.com/hc/en-us/requests/new and we'll fix this issue for you!

REST API - HTTP status codes with multiple errors

I'm currently in the process of creating a small API. I have some error conditions, the 3 in question in this case are:
The user making a request with any method other than POST
The user not being authenticated
An entity not being found; resulting in no action being able to be made.
In that order. I had originally decided that I could assign a status code to each of these errors, (i.e. 400, 403, and 404, in that order) but then realised that I can't set multiple HTTP status codes.
How does one deal with this issue? Should I use HTTP status codes?
In my view it should check each of these conditions in the order you specified and return immediately with the corresponding error code if one of the conditions fail.
So only 1 error code will be returned.
It would be OK to use HTTP status codes, but it depends on who is consuming your API. Sometimes it is better to just return 200 OK and then include Error information in the body.
With Status Codes
If you go with status codes just return the first error encountered, no use in handling the request further anyways, so in pseudo:
if (request is not POST) return 405; //abort here
//we know request is POST here
if (request not auhtorized) return 401; //abort here
//we know request is POST and authorized
if (request requests a not exisiting entity) return [404, 422, ..., 5xx] either will do; // abort here
// we now know the request is POST, autorized and requests valid information
processRequest();
Without Status Codes
As an alternative, since you tagged ajax, I assume you are returning JSON, so just return 200 OK and include a the fields success : [true|false] and errorMessage : ["Not POST"|"Bad Auth"|"Bad Request or Unknown resource"|"OK"] in your JSON answer.
You could also combine both ways, but depending on the ajax client not all will work well with all status codes. Given the information in the answer, all you need to do is check if success === true and handle error otherwise.

Pattern for responses from HTTP JSON API

I have a HTTP JSON API, which runs on php, on a small framework. This API is a wrapper for a databases pgsql functions.
Php framework returns responses in such way:
{
code: 200,
data: []
}
Codes are HTTP code responses (such as 200, 301, 302, etc). pgsql functions returns their own code (negative values for errors, positive for success results), message (meaning of code) and result data:
{
code: -1,
message: 'Wrong data',
data: []
}
So, my packages from API are:
{
code: 200,
data: {
code: 1
message: 'Succeed'
data: []
}
}
Isn't it messy?
Occur some confusions when writing client code, that requests this API.
Maybe there are some standard patterns for making some kind of packages of API.
Your API layout is not messy. As Botond suggested, it is actually pretty logical. The only change I would make to it would be to move your status codes into HTTP headers rather than in the JSON data, to reduce the format a bit. This will also allow you to easily differentiate between successful calls and errors.
Suppose your API can answer with 4 different codes: 200, 201, 403, 404. Respectively: done, not changed, forbidden, not found. Instead of passing this as a JSON variable, you could easily bind it into the HTTP response header, as the values already exist and are well understood. This, as in this question, is a pretty well-accepted method of providing status codes, provided that you are not using this specific header for anything else.
See you have to read the responses in Iterative manner. You can read the JSON response and then check if the data field has another object/array.
You have to assess the code and show error messages on all codes except 200.

Categories