Error trying to create webhook - php

I tried to create a webhook for a task in Asana but the only response I'm getting is this:
{
"errors": [
{
"message": "Could not complete activation handshake with target URL. Please ensure that the receiving server is accepting connections and supports SSL",
"help": "For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"
}
]
}
(Status: 400 Bad Request)
I am sending a POST Request via Postman to https://app.asana.com/api/1.0/webhooks with following content:
{
"data":
{
"resource": 123456789012345,
"target": "https://example.com/asana.php"
}
}
The asana.php looks something like this:
$headers = getallheaders();
$secret_token = $headers['X-Hook-Secret'];
header('X-Hook-Secret: ' . $secret_token);
What am I doing wrong? Am I missing something?

According to the Asana API Reference (https://asana.com/developers/api-reference/webhooks),
The target must respond with a 200 OK and a matching X-Hook-Secret header to confirm that this webhook subscription is indeed expected.
When you send the header, do you know what response code is being sent? Perhaps you might want to have a look at the $http_response_code argument in http://php.net/manual/en/function.header.php

Related

DialogFlow V2 API - Webhook response was empty

My webhook is not getting invoked from Dialogflow and always returning an error
Webhook call failed. Error: Webhook response was empty.
When I use CURL/POST MAN with the dialogflow request the response I get is according to the documentation.
{
"fulfillmentMessages": [
{
"text": {
"text": [
"Hi, how can I help?"
],
}
}
],
"fulfillmentText": "Hi, how can I help?",
"outputContexts": [],
}
Check to make sure the URL you have set for fulfillment is the same one you're using in your tests and that it responds correctly to POST messages that are formatted with the webhook request JSON.
Check your server logs to see if there is an error being generated when your webhook is called.

Issue with google calendar push notification

I tried implementing the google calendar push notification in my server.
Using the outhplayground, i was able to successfully subscribe to the service.
I am getting notifications to my registered url when a change takes place in the calendar.
The only issue is that the response that i receive doesnt have data. Its an empty response.
Could anyone tell me what the issue would be. I am using PHP code in the backend to access the request hitting my url.
authplayground code:
POST /calendar/v3/calendars/calendarname#gmail.com/events/watch HTTP/1.1
Host: www.googleapis.com
Content-length: 161
Content-type: application/json
Authorization: Bearer access_token
{
"id": "01234267-89a6-cdef-0123456789ab", // Your channel ID.
"type": "web_hook",
"address": "https://example.com/response" // Your receiving URL.
}
Code to accept request:
$json = file_get_contents('php://input');
$request = json_decode($json, true);
$post_request = $_POST;
$get_request = $_REQUEST;
As I was getting an empty response, i tried writing the code to accept any possible way.
Google sends the response in the headers as an array.
Try this:
$response = apache_request_headers();
if($response) {
print_r($response);
}else {
echo 'The apache_request_headers() did not find any headers.'; //Or google is not sending any.
}
You may also try:
getallheaders()
if the apache_request_headers did not work.
Testing this can be difficult. You may want to set up a log that sends any data your page gets to a table on your database so that you can go back and inspect to see what type of progress you are making.
You can get the values like this:
$channelId = $_SERVER['HTTP_X_GOOG_CHANNEL_ID'];
$resourceId = $_SERVER['HTTP_X_GOOG_RESOURCE_ID'];
if($channelId) {
file_put_contents('webhook.txt', $channelId.' ||| '.$resourceId);
}
You can get the different headers here: https://developers.google.com/calendar/v3/push#understanding-the-notification-message-format

DialogFlow (API.AI) REST with php

I work on DialogFlow recently and I need to send a POST to my own web services.
It's a Rest services work with Symfony et PHP.
So I tried something and it didn't work.
In the tutorial it worked with Google Cloud, but I don't want to work with it.
I changed the URL of Webhook with my own. I didn't change anything else in DialogFlow because it was good with google. There it is :
/**
* #Rest\View()
* #Rest\Post("/testDialogBot")
*/
public function testDialogBotAction( Request $request )
{
$re = "Test reponse";
$response = new Response(json_encode( array( "speech" => $re, "displayText" => $re )));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
The JSON of DialogFlow return :
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Request timeout."
},
I'm sure it can work, I do something bad probably.
Thank's for help.
According to the official docs the response "should" have the following fields: speech, displayText, data, contextOut adn source. You are only sending speech and displayText. Maybe adding the others will do the trick.
Also, the limits stated on the docs for the response are:
Timeout for service response – 5 seconds. Data received in the
response from the service – up to 64K.
Check if your server can send the response within those parameters
Ok that's fine, you don't need every parameters. My URL was wrong and It seems that DialogFlow doesn't accept https but only http.

Wrong response from the webhook: 400 Bad Request

I'm recently tried to use webhook to get update from telegram. my program work correctly whit getUpdates().
but when i set webhook i got
"Wrong response from the webhook: 400 Bad Request"
error when try to check status of webhook by getWebhookInfo method.
here is my code:
$telegram->commandsHandler(true)
when is used below code in getUpdates mod every thing was fine.
$telegram->commandsHandler(false)
And is should say i use https and my ssl is ok.
This is answer of getWebhookInfo to me.
{
"ok": true,
"result": {
"url": "https://telbit.ir/api/bot/<token>",
"has_custom_certificate": false,
"pending_update_count": 13,
"last_error_date": 1476344420,
"last_error_message": "Wrong response from the webhook: 400 Bad Request"
}
}
I found my answer
every things was ok. the error happens because of my framework.

Proper mechanism for sending PHP errors to the client

Greetings,
I was trying to discover a proper way to send captured errors or business logic exceptions to the client in an Ajax-PHP system. In my case, the browser needs to react differently depending on whether a request was successful or not. However in all the examples I've found, only a simple string is reported back to the browser in both cases. Eg:
if (something worked)
echo "Success!";
else
echo "ERROR: that failed";
So when the browser gets back the Ajax response, the only way to know if an error occurred would be to parse the string (looking for 'error' perhaps). This seems clunky.
Is there a better/proper way to send back the Ajax response & notify the browser of an error?
Thank you.
You could send back an HTTP status code of 500 (Internal server error) , and then include the error message in the body. Your client AJAX library should then handle it as an error (and call an appropriate callback etc.) without you having to look for strings in the response.
I generally send back a JSON response like this:
$response = array('status' => 'error', 'message' => 'an unknown error occured');
if( some_process() ) {
$response['status'] = 'success';
$response['message'] = 'Everything went better than expected.';
} else {
$response['message'] = "Couldn't reticulate splines.";
}
die( json_encode($response) );
So, I can check the status of response.status in my JavaScript and look for a value of "sucess" or "error" and display response.message appropriately.
showing to the user a status of what is happening and what has happened is very important.
I you want to structure your ajax responses, you should look into the json format.
if (something worked)
echo '{ "error": 0 }';
else
echo '{ "error": 1 }';
Once you set foot into the json world, you will be able to send more structured output. For example :
if (something worked)
echo '{ "error": 0 }';
else
echo '{ "error": 1, "code": 889, "desc": "Something bad happened" }';
When you receive this output in javascript, you can transform it into an object and take actions depending on the different keys.
The library json2.js will help you transform your output into an object
Sending the appropriate http headers should do the trick and tell your ajax scripts to execute the right callback. Each javascript framework i know of has a success and error callback for it's XHR requests.
header('HTTP/1.1 500 Internal Server Error');
You can send back a JSON object which contains a custom error code and error message which you can then either handle or display directly to your users:
{
"response": 10,
"message": "The database didn't work or something"
}
It would work for success as well:
{
"response": 1,
"message": "It worked! Yippee!"
}

Categories