Telegram-Bot answerInlineQuery no messageID as result - php

using the below code sniplet to send an InlineQuery to a chat (or channel or group) in Telegram as answer to a "Share"-button from another chat - it seems to work well...
My inline bot creates a message and places it into the target chat.
Problem is: I do not get a message-ID or similar back which allows me to acces this message again in order to be able to modify it.
(Goal is to synchronize content between several channels even if no bot is part of the the channel and the content has been shared via "Share"-inline-buttons).
I.e. $res in the sample below is $res = {"ok":true,"result":true}
Any idea, what can be done?!
Thanks!
$botID = 'botabcdefghij1234567890';
$url = "https://api.telegram.org/$botID/answerInlineQuery";
$results = array(
array(
"type" => "article",
"id" => $iid,
"title" => $title,
"description" => $desc,
"reply_markup" => $reply, // some buttons here
"input_message_content" => array(
"message_text" => "$txt", // synchronized text
"parse_mode" => "HTML"
)
)
);
$post = array("inline_query_id" => $iid, "results" => json_encode($results));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$res = curl_exec($ch);
curl_close($ch);

You can send multiple answers in one inline query, so please enable /setinlinefeedback in #BotFather to receive messages ID.
It will return chosen_inline_result update, then use inline_message_id to modify the message.

Related

Inviting people with Trello API

i'm trying to invite a person via email to Trello from my website. Here is the API reference. When I try to invite him, the plain reply is "invalid key". Here is my function:
public function inviteEmployeeToTrello ($email, $name, $isAdmin)
{
$organazationTrelloID = 'myOrganazationID';
$trelloAuthToken = 'myTrelloAuthToken';
$trelloInviteUrl = 'https://trello.com/1/organizations/'.$organazationTrelloID.'/members';
if ($isAdmin == 1)
{
$type = 'admin';
}
else
{
$type = 'normal';
}
$fields = array(
'fullName' => $name,
'email' => $email,
'type' => $type,
'token' => $trelloAuthToken
);
// open connection
$ch = curl_init();
// set the url, number of PUT vars, PUT data
curl_setopt($ch, CURLOPT_URL, $trelloInviteUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// exec
$replyRaw = curl_exec($ch);
$reply = json_decode($replyRaw, true);
// close connection
curl_close($ch);
dd($ch);
}
CURLOPT_POSTFIELDS does not want a JSON,
if you want a urlencoded request, use http_build_query($fields) , or if you want a multipart/form-data request, just give it $fields array directly. (the API doc's doesn't seem to mention which request types it accept, though. urlencoded is the most common one.)
As the error code says, you forgot to pass your application key. Here's an example from the API reference :
https://api.trello.com/1/organizations/publicorg?members=all&member_fields=username,fullName&fields=name,desc&key=[application_key]&token=[optional_auth_token]
You have to include it in your query, hence in your case, add it to the
$fields array :
$fields = array(
'fullName' => $name,
'email' => $email,
'type' => $type,,
'key' => $trelloAppKey
'token' => $trelloAuthToken
);

Group key onesignal push notification

created a project on one signal.
I can send to php notification on my phone.
The problem comes when it arrives more than one notification. The new notification replaces the previous notification (not yet read ).
How do you get instead say that Android has 2 unread notifications?
I have try write the same android_group but the notification never stacked and the newest continue replace the previus.
This is my code:
<?php
function sendMessage(){
$content = array(
"en" => 'text message test'
);
$fields = array(
'app_id' => "XXxxxxXX-xxxxXX-XXxxX-Xxxx-XxxxXXx",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'headings' => array("en" => "Test message!!"),
'android_group' => 'TESTGROUP',
'android_group_message' => array("en" => "message"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
?>
How can i solve this problem?
android_group used to enable notification stacking only works for Android apps.
If you are you're web push with Chrome for Android the replacement behavior your seeing is expected and isn't configurable. Same behavior on Chrome for Desktop and Firefox.
Also note android_group_message should contain $[notif_count] so the number of unread messages is seen. Example
array("en" => "You have $[notif_count] new messages")

unable to send out voice broadcast with callfire REST API

I'm trying to send out an outbound call with the callfire REST API and am having some difficulty doing so. Here's my code (adapted from https://developers.callfire.com/docs.html#createVoiceBroadcast):
<?php
$username = '...';
$password = '...';
$data = array(
'name' => 'Automation Test',
'fromNumber' => '...',
'recipients' => array(
array('phoneNumber' => '...')
),
'answeringMachineConfig' => 'AM_AND_ALIVE',
'liveSoundText' => 'hello, world!',
'machineSoundText' => 'hello, world!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.callfire.com/v2/campaigns/voice-broadcasts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = json_decode(curl_exec($ch));
print_r($result);
The problem is in the response. It's as follows:
stdClass Object
(
[httpStatusCode] => 415
[internalCode] => 0
[message] => Exception in API
)
The 415 status code is for "Unsupported Media Type" per https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error but since I'm not uploading any media that error doesn't really make a lot of sense.
Maybe my value for answeringMachineConfig is invalid. idk what AM_AND_LIVE is supposed to mean but it's in the example so I'm using it. If there's only a small number of possible values the documentation should say so..
You need to set content type to 'application/json'.
Old I know, but just ran across it. Perhaps:
'answeringMachineConfig' => 'AM_AND_ALIVE',
should be:
'answeringMachineConfig' => 'AM_AND_LIVE',
You wrote ALIVE instead of LIVE

I need help creating an endpoint that can receive json data

I have created a php file that sends json data to an external url. Their API requires that I have a page on my website that receives the json responses for processing. The one that sends json data works well. I need help with the second php page that should process it
$Url="http://example.com/submit.php";
$date = date_create();
$UserID=7;
$Password='';//<-password written here
$Timestamp=date_timestamp_get($date);
$token=$UserID.$Password.$Timestamp;
$data_string = array();
$data_string = array(
"AuthDetails" => array(
array(
"UserID" => $UserID,
"Token" => md5($token),
"Timestamp"=>$Timestamp
)
),
"MessageType"=> array(
"3"
),
"BatchType"=>array(
"0"
),
"SourceAddr"=>array(
"Example"
),
"MessagePayload"=> array(
array(
"Text" => "Sample text message by Example :)"
)
),
"DestinationAddr" => array(
array(
'MSISDN'=>'254701000000',
'LinkID'=>''
)
),
"DeliveryRequest" => array(
array(
'EndPoint'=>'',//<-URL that receives the response
'Correlator'=>md5(uniqid())
)
)
);
$data_string=json_encode($data_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Request Error:' . curl_error($ch);
}
curl_close($ch);
return $output;
Ok this is going to be a bit generic but it should put you on the right path.
So you have to write a script that will process the response from them i.e. your end-point for this circle of events.
So lets call it my-enpoint.php
So in the message you send to them you put the address of this new script into this parameter
"DeliveryRequest" => array(
array(
'EndPoint'=>'http://www.example.com/my-endpoint.php',
'Correlator'=>md5(uniqid())
Now your script my-endpoint.php I am assuming they have said how they will return their reply, probably as POST variable. You process their reply basically like you would a submitted form from your own site.
So for example if their reply is POSTed
<?php
// initial testing to see what comes back from them
// as this wont be associated with a browser
// dump their reply to a file so you can see whats there
file_put_contents('reply.txt', print_r($_POST, true), FILE_APPEND);
?>
With the info you have provided this is about a much as I can do.

Why is this PHP script the JSON results when I haven't told it to?

So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.

Categories