Since 2 days I've been exploring the Telegram bot API, which is pretty neat. But there is one thing I can't figure out.
When you don't use the webHook but the /getUpdates call, you can tell the API via the offset parameter which message where processed by the server.
But how do you do this with the webHook in place? I keep getting the same message as an update. Which results in the server spamming the user with the same message.
The solution I came up with is as follows:
Receive an update from the webhook
Save the update_id
Reply to the user /sendMessage
disable the webHook /setWebhook?url=
Set the offset /getUpdates?offset={update_id+1}
Reinstate the webHook /setWebhook?url=https://mywebhook.domain.com
There must be a better way right? Anyone?
Ok, problem solved. It appeared that just a 200 (OK) wasn't enough (the body of my response was null. I've added a body to the response {}, and know it works fine.
You must say to telegram that you get updates successfully with this:
- 200 response code
&
- empty json like this {}
use This on webHook to get data from telegram servers:
// get the raw POST data
$rawData = file_get_contents("php://input");
// this returns null if not valid json
$jsonData = json_decode($rawData);
What HTTP status code are you returning on the page handling your webhook? It is possible that Telegram is attempting to retry your webhook endpoint because it's not receiving a status 200 (OK) from you.
Related
When an app fails to handle a Slack slash command, the text is brought back to allow the user to edit it and immediately send it again. I have a slash-command that searches, but might fail to find any results. In that case, I would like the user to immediately be able to modify their search. The Slack docs explain that I should always return a 200 HTTP status, but then Slack also erases the command and the user can't immediately try again. When I tried to respond with a 404 status, the users get an alarming message like failed with the error "http_client_error". Is there a way to fail but also provide a custom message to the user why?
Yes, but you must not use HTTP status codes to community a failed search.
Just always return HTTP OK 200 and then add a response message telling the user what went wrong. You can do that by directly replying to the request from Slack within 3 seconds, or alternatively by sending a message to the response_url.
This is also clearly expressed in the offical documentation for slash commands:
Sending Error Responses
There are going to be times when you need to let the user know that something went wrong - perhaps the user supplied an incorrect text parameter alongside the command, or maybe there was a failure in an API being used to generate the command response.
It would be tempting in this case to return an HTTP 500 response to
the initial command, but this isn't the right approach. The status
code returned as a response to the command should only be used to
indicate whether or not the request URL successfully received the data
payload - while an error might have occurred in processing and
responding to that payload, the communication itself was still
successful. (Source)
As far as I know it is not possible to signal Slack that the user should be able to edit his last command though.
API Testing:
Currently, I am using Postman to test API response
I want to monitor a particular text in API response and get notified for example-
{
"productname": "PARLE",
"customer": "ABC",
}
If I get a customer name in the API response as ABC I want to get notified through mail or slack or anything.
Is this possible? if Yes please share me the inputs.
you can run periodic tests with a software like Overseer, and receive notifications using a Notify17 notification template (see the sample recipe).
You could use a test rule like:
http://myurl.com/path must run http with not-content '"customer": "ABC"'
To have an easy start with Overseer, you can check out the Kubernetes deployment example.
You can achieve your use case using Postman Monitors to send an email or send a slack message by following these steps:
Configure your monitor to run with an environment. (Reference: https://learning.getpostman.com/docs/postman/monitors/intro_monitors/)
In the test script of your Collection's Request, fetch the response using pm.response.json() (Based on the response structure you mentioned)
Use the following code snippet to determine whether the response contains what you need:
if (pm.response.json().customer === 'ABC') {
// no op
}
else {
postman.setNextRequest(null);
}
Here, if the condition is not met, then the next request that will be executed is null, which means that the collection execution will stop here. However, if the condition is met, this will not be set and the next request will be executed.
You can use various Public APIs to achieve tasks like send an email or send a slack message:
Gmail API | Slack API
Create a request below the current request titled 'Send notification'. Use the documentation provided to set the request up.
When your monitor runs, if the condition is not met, then postman.setNextRequest will be set to null and the 'Send notification' request will not run. However, if the condition is indeed true, then the request will run and you will receive a notification on the respective channel.
I am using 'order/create' hook
it is created fine through app. but when order is placed , hook url is called from api many times.
Using php, how i can make it call once only hook url.
I am sending response header of 200 ok also here is my api file code
$checkout_information = json_decode(file_get_contents(
'php://input'));
ob_start();
print_r($_REQUEST);
print_r($checkout_information);
$out1 = ob_get_contents();
ob_end_clean();
mail('xxxx#xxx.com','Testing Connect shopify order create',$out1);
$status['status'] = '200';
http_response_code(200);
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
echo json_encode($status);
exit();
Any body to help me out in this situation.
How long does your app take to return the 200? Shopify webhook docs state that they should get a response within 5 seconds, or they will retry. Seems quite possible that sending an email may take more than 5 seconds.
Remember that you have to account for latency to/from Shopify in addition to your own processing. I've seen them retry webhooks even with sub 2 second processing.
Ideally, you should place the webhook payload/topic on some sort of queue & perform any processing outside the HTTP request.
https://docs.shopify.com/api/webhooks/using-webhooks
I am developing a RESTful API. I am confused about setting HTTP status code in this particular scenario. I am not sure what status code should I (server) return.
Let's say my app has a follow user functionality, if I am already following a user and again I send follow request for the same user id then in this case what should be the HTTP status code from server. The status code will be followed by an error message saying something like: "already following the user."
Similar scenario can be considered for unfollow user functionality, if I am not following an user "A", still I send request to unfollow user "A", then what HTTP status code should server return with error message something like "not following user to unfollow"
Certainly 200 response code doesn't seem to be appropriate to me here? or does it?
Please forgive me if I have posted the question at wrong stack exchange site, I posted it in stackoverflow site just because it is related to REST APIs.
EDIT
From client side user needs to send POST request to the URL:
http://www.myserver.com/api/follow/10
along with other necessary parameters ( like API keys, proper headers, etc) which are used for authentication before serving the requests at server side.
similar URL for unfollow action is:
http://www.myserver.com/api/unfollow/10
Right now, I am sending HTTP status code 200 in response if the client sends follow request, let's say, for user id 10 even if he/she is already following the user with id 10. In this case,along with status code (200) I am sending message similar to "already following the user"
Somehow I feel this is not convincing as no resource is created/updated it should return the error message with proper status code something other than 200, may be one from 4XX, not sure.
422 Unprocessable Entity
422 seems to be the proper HTTP status code in this use case.
The description of 422 says:
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions.
The answer depends on your API. You're describing the API in terms of "follow user X" or "unfollow user Y". That makes me think you might be approaching your API design in an RPC style rather than focusing on resources.
If your API uses REST including the HATEOAS principle, then error codes from the 4xx range may be appropriate (but I would recommend against it in this case, see below). In very short: HATEOAS means that your resources provide links to possible "actions". You can read more about it here: http://restcookbook.com/Basics/hateoas/
Apart from that, it seems a good idea to design your API "fault tolerant", i.e. expect the same request sent multiple times (e.g. because users are impatient and click again and again, or the browser crashed and is restarted and reopens all previous tabs, or...).
My personal opinion and recommendation is the following:
follow user X: Your implementation should check if it needs to add the new follower or not. Regardless, if the user is already following or not, send back HTTP status 201 (created) and add the "Location" HTTP header pointing at the resource.
unfollow user X: Your implementation should check if it needs to delete the follower or not. Regardless, if the user is already removed from the followers or not, send back HTTP status 200 (OK).
The general idea is, if a client requests something to be a certain way and that is already the case, the server has two options: Either it responds to the client "The result you wish is already in place. Therefore your request is invalid." or the server can respond "The result you wish is already in place. You have everything you need.".
Going for the second option makes the API more tolerant and helps with idempotency (see http://restcookbook.com/HTTP%20Methods/idempotency/).
I think djlauk's answer covers a lot, but I want to give a little different approach and add some information:
Do not use verbs in the URI
I would not use POST on /follow/ respectively /unfollow/ URIs because this is not very RESTful see this SO question: Why does including an action verb in the URI in a REST implementation violate the protocol? and escpacially this SO answer: How to create REST URLs without verbs?
Do use the correct HTTP verbs for the actions
What you want to do is a creation of an entity ("follow") so for that you can use the HTTP verbs POST or PUT and afterwards the deletion of that entity ("unfollow") where DELETE would be the right fit.
My approach for your API:
I would do the following:
(The first two examples are just for explaining the structure, you don't have to implement them if you don't need them.)
This does get you the user "robert":
GET http://www.myserver.com/api/users/robert/
response: #200
This does get you the users "robert" is following:
GET http://www.myserver.com/api/users/robert/following/
response: #200
And this is how you let "robert" follow "rahul":
PUT http://www.myserver.com/api/users/robert/following/rahul
response: #200
If you send this request again you get the same response:#200 because PUT is idempotent and this is how it should behave (see (2))
When you now want to let "robert" unfollow "rahul" you send:
DELETE http://www.myserver.com/api/users/robert/following/rahul
response: #200
If you send the DELETE request again you get a little different response a #404 , but this is HTTP standard and the clients should understand this.
For the regular answer codes of HTTP methods I can also recommend this source: restapitutorial.com
I would use some of the following:
System.Net.HttpStatusCode.ServiceUnavailable;
System.Net.HttpStatusCode.MethodNotAllowed;
System.Net.HttpStatusCode.BadRequest;
Better if it is one of the first two.
Certainly 200 response code will not work in this situation.
following are the groups in HTTP Status Code:
1xx Informational
2xx Success
3xx Redirection
4xx Client Error
5xx Server Error
Certainly you need to use 4xx.
I think for the condition that you have described here, you can use any of the following:
405 Method Not Allowed
A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.
400 Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error
409 Conflict
Indicates that the request could not be processed because of conflict in the request, such as an edit conflict in the case of multiple updates.
More details are available here:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
I am creating a Rails 3.2 app and I am using Paymill as the payment gateway.
I am trying to setup a webhook on my system (Already setup on Paymill side). This webhook should respond to callbacks when a transaction was successful.
How can I "capture" the response object in my code? Using params?
Thankful for all help!
I don't know paymill, but it looks like it works the same way as stripe.
Thus, you have to handle the response with params.
You can have a look on this code sample: https://github.com/apalancat/paymill-rails
A webhook call from Paymill includes a JSON in the request. This JSON includes some meta data about the event that was triggered and the objects affected. So, you'd have to take the request body and parse the JSON to extract the information you are looking for. You can see a sample JSON file here:
https://www.paymill.com/de-de/dokumentation/referenz/api-referenz/#events