Telegram Inline Bot shows nothing inline with inlinekeyboard php - php

I have problem with inline bot
my bot work without 'reply_markup' when I use 'reply_markup' nothing to show
function buildInlineKeyboard(array $options) {
$replyMarkup = array(
'inline_keyboard' => $options,
);
$encodedMarkup = json_encode($replyMarkup, true);
return $encodedMarkup;
}
$keyboard = $tel->buildInlineKeyboard(array(
array(
$tel->buildInlineKeyboardButton('View Message', '', 'viewNews_')
)
));
$item[] = array(
"type" => "article",
"id" => (string)++$i,
"title" => $plays['team1'] . " - " . $plays['team2'],
'description' => $title . ' - ' . $news['title'],
'input_message_content' => array(
'message_text' => $news['title'],
'parse_mode' => 'HTML'
),
'reply_markup' => $keyboard
);
when I comment 'reply_markup' bot work an show articles and when I use 'reply_markup' nothing to show in inline mode
I set webhook no error to show
$keyboard work with sendMessage

Make sure that the format of the reply_markup looks as following. There's probably something messed up with the arrays. You also need to json_encode the reply_markup.
array(1) {
["inline_keyboard"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(2) {
["text"]=>
string(12) "View Message"
["callback_data"]=>
string(9) "viewNews_"
}
}
}
}

no the array is correct I update post and add buildInlineKeyboard function

Related

Why the SOAP request is empty?

I develop the system to export some data from the client's side using the SOAP. I have a link to their staging wsdl, and implemented some kind of the SOAP client, but unfortunately my SOAP request is empty and the response is the error one.
Link to WSDL: https://rewardsservices.griris.net/mapi/OrderManagementServices.svc?wsdl
Operation called: exportPendingOrder
Snippet of my SOAP Client:
$soap = new \SoapClient('https://rewardsservices.griris.net/mapi/OrderManagementServices.svc?wsdl', [
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => 1,
'exception' => 1,
]);
​
$headers = [
new SoapHeader(
'http://www.w3.org/2005/08/addressing',
'Action',
'http://tempuri.org/IOrderManagementServices/exportPendingOrder',
true
),
new SoapHeader(
'http://www.w3.org/2005/08/addressing',
'To',
'https://rewardsservices.griris.net/mapi/OrderManagementServices.svc',
true
),
];
​
$soap->__setSoapHeaders($headers);
​
try {
$params = [
'parameters' => [
'merchantNetworkID' => "XXX",
'merchantCode' => "XXX",
'subProgramNetworkID' => "XXX",
'countryISOCode' => "XXX",
'grToken' => "XXX",
'requestId' => (new \DateTime())->getTimestamp(),
],
];
​
$result = $soap->exportPendingOrder($params);
​
var_dump([
'params' => $params,
'result' => $result,
'request' => $soap->__getLastRequest(),
'response' => $soap->__getLastResponse(),
]);
} catch (\SoapFault $exception) {
var_dump([
'error_message' => $exception->getMessage(),
'request' => $soap->__getLastRequest(),
'response' => $soap->__getLastResponse(),
]);
}
Log information (incl. the request/response):
array(4) {
["params"]=>
array(1) {
["parameters"]=>
array(6) {
["merchantNetworkID"]=>
string(36) "XXX"
["merchantCode"]=>
string(3) "XXX"
["subProgramNetworkID"]=>
string(36) "XXX"
["countryISOCode"]=>
string(2) "XXX"
["grToken"]=>
string(110) "XXX"
["requestId"]=>
int(1619772724)
}
}
["result"]=>
object(stdClass)#185 (1) {
["exportPendingOrderResult"]=>
string(121) "{"responseCode":"1002","description":"Required field value missing","result":{"requestID":null,"serializedDataset":null}}"
}
["request"]=>
string(496) "<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://www.w3.org/2005/08/addressing"><env:Header><ns2:Action env:mustUnderstand="true">http://tempuri.org/IOrderManagementServices/exportPendingOrder</ns2:Action><ns2:To env:mustUnderstand="true">https://rewardsservices.griris.net/mapi/OrderManagementServices.svc</ns2:To></env:Header><env:Body><ns1:exportPendingOrder/></env:Body></env:Envelope>
"
["response"]=>
string(531) "<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://tempuri.org/IOrderManagementServices/exportPendingOrderResponse</a:Action></s:Header><s:Body><exportPendingOrderResponse xmlns="http://tempuri.org/"><exportPendingOrderResult>{"responseCode":"1002","description":"Required field value missing","result":{"requestID":null,"serializedDataset":null}}</exportPendingOrderResult></exportPendingOrderResponse></s:Body></s:Envelope>"
}
Could you please advise what I do wrongly, and why my SOAP request is empty basing on the wsdl provided? Any help is appreciated!
Thanks in advance,
Yevhen
Finally I have managed to send the non-empty request. I have checked the partner's wsdl using the SoapUI tool and it showed me the correct format of the request. So the correct request has to be the following one:
...
$params = [
'JsonData' => json_encode([
'merchantNetworkID' => "XXX",
'merchantCode' => "XXX",
'subProgramNetworkID' => "XXX",
'countryISOCode' => "XXX",
'grToken' => "XXX",
'requestId' => (new \DateTime())->getTimestamp(),
]),
];
...

Telegram Inline keyboards PHP

I would like to know how to change the text when clicked button attached to it (Inline keyboards). It's in a telegram channel.
Something like this but with my code below (no need for more options).
The code I have now:
$data = [
'text' => 'choose options yes or no',
'chat_id' => '-100234234234'
];
$keyboard = array(
"inline_keyboard" => array(
array(
array(
"text" => "Yes",
"callback_data" => "myCallbackData"
),
array(
"text" => "No",
"callback_data" => "myCallbackData"
)
)
)
file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) . "&parse_mode=html&reply_markup=$keyboard");
After sending the message;
Remember the message_id returned by Telegram
Call /getUpdates to get the callback_data of pressed button
Use /editMessageText to update the first message
Example;
<?php
// Create data
$data = http_build_query([
'text' => 'Yes - No - Stop?',
'chat_id' => '1234567890'
]);
// Create keyboard
$keyboard = json_encode([
"inline_keyboard" => [
[
[
"text" => "Yes",
"callback_data" => "yes"
],
[
"text" => "No",
"callback_data" => "no"
],
[
"text" => "Stop",
"callback_data" => "stop"
]
]
]
]);
// Send keyboard
$url = "https://api.telegram.org/bot$token/sendMessage?{$data}&reply_markup={$keyboard}";
$res = #file_get_contents($url);
// Get message_id to alter later
$message_id = json_decode($res)->result->message_id;
// Continually check for a 'press'
while (true) {
// Call /getUpdates
$updates = #file_get_contents("https://api.telegram.org/bot$token/getUpdates");
$updates = json_decode($updates);
// Check if we've got a button press
if (count($updates->result) > 0 && isset(end($updates->result)->callback_query->data)) {
// Get callback data
$callBackData = end($updates->result)->callback_query->data;
// Check for 'stop'
if ($callBackData === 'stop') {
// Say goodbye and remove keyboard
$data = http_build_query([
'text' => 'Bye!',
'chat_id' => '1234567890',
'message_id' => $message_id
]);
$alter_res = #file_get_contents("https://api.telegram.org/bot$token/editMessageText?{$data}");
// End while
break;
}
// Alter text with callback_data
$data = http_build_query([
'text' => 'Selected: ' . $callBackData,
'chat_id' => '1234567890',
'message_id' => $message_id
]);
$alter_res = #file_get_contents("https://api.telegram.org/bot$token/editMessageText?{$data}&reply_markup={$keyboard}");
}
// Sleep for a second, and check again
sleep(1);
}
Note:
This example is written based on OP's code, just to show the idea of altering an inline_keyboard.
This code is purely as an example, there should be a lot more error checking etc...
Based on the comment, I've included a while true to keep on checking for new press.

Request URI Too Long with POST REST

I have the following peice of PHP code, using to access a POST method REST web service :
$PJS = isset($_REQUEST['arrayFile']) ? $_REQUEST['arrayFile'] : array();
foreach ($PJS as $PJ) {
$ext = explode('/', $PJ['type'])[1];
$storeAttachmentResourceParams = array(
'encodedFile' => $PJ['content'],
'resId' => $resId,
'data' => json_encode(
array(
array('column' => 'title', 'value' => 'PJ', 'type' => 'string'),
array('column' => 'attachment_type', 'value' => 'simple_attachment', 'type' => 'string'),
array('column' => 'status', 'value' => 'A_TRA', 'type' => 'string'),
)
),
'collId' => 'letterbox_coll',
'collIdMaster' => 'letterbox_coll',
'table' => 'res_attachments',
'fileFormat' => $ext
);
$storeAttachmentResource = Requests::post($cfg['url'] . '/attachments', array(), $storeAttachmentResourceParams, $options);
This piece of PHP code is call under a wordpress PHP snippet like the following :
$url = 'https://XXXX.XXX.fr/interface/iface.php';
$options = array(
'http' => array(
'header' => "",
'proxy' => "tcp://192.168.X.X:3128",
'timeout' => 100,
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$return = file_get_contents($url, false, $context);
$returnIface = json_decode($return);
I modify my Apache2.conf like the following to add the LimitRequestLine:
AccessFileName .htaccess
LimitRequestLine 10000000
LimitRequestFieldSize 10000000
Restarted the Apache2 server but still have the following error in my browser :
Request-URI Too Long
The requested URL's length exceeds the capacity
limit for this server.
Apache/2.4.25 (Debian) Server at infosv47.sartrouville.lan Port 80
"
["headers"]=>
object(Requests_Response_Headers)#23 (1) {
["data":protected]=>
array(4) {
["date"]=>
array(1) {
[0]=>
string(29) "Fri, 06 Jul 2018 13:03:25 GMT"
}
["server"]=>
array(1) {
[0]=>
string(22) "Apache/2.4.25 (Debian)"
}
["content-length"]=>
array(1) {
[0]=>
string(3) "339"
}
["content-type"]=>
array(1) {
[0]=>
string(29) "text/html; charset=iso-8859-1"
}
}
}
["status_code"]=>
int(414)
["protocol_version"]=>
float(1.1)
["success"]=>
bool(false)
["redirects"]=>
int(0)
["url"]=>
string(46) "http://10.10.XX.XX/cs_maarch/rest/attachments"
["history"]=>
array(0) {
}
["cookies"]=>
object(Requests_Cookie_Jar)#20 (1) {
["cookies":protected]=>
array(0) {
}
}
}
It's under a Debian 9

Telegram PHP inline_keyboard

I'm trying to create a bot which send an inline_keyboard when in receive the text "/start", the problem is that i can't see the response when i use this function to send the keyboard
function sendKeyboard($chat_id, $text) {
$keyboard = ['inline_keyboard' => [
['text':'Yes'],
['text':'No']
],
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true
];
$keyboard = json_encode($keyboard);
$url = $GLOBALS[website] . "/sendMessage?chat_id=".$chat_id."&
reply_markup=".$keyboard."&text=".urlencode($text);
file_get_contents($url);
}
Can somebody understand how to solve this problem?
Inline Keyboard buttons is array of array of Button, and resize_keyboard, one_time_keyboard and selective is not for inline keyboard, it's parameters for Reply Keyboard.
Your code only have array of Button, and Button only have text field, it need to add callback_data or url, or you will get error.
You have better to see reference about details.
function robot($method,$datas=[]){
$url = "https://api.telegram.org/bot".API_KEY."/".$method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
robot('sendmessage', [
"chat_id" => $chat_id,
'message_id'=>$messageid,
"text" => "* buttonwith out link *
",
'reply_markup' => json_encode([
"one_time_keyboard" => true,
'inline_keyboard'=> [
[
['text' => "button 1", 'callback_data' => "buttoncode-39500"]
]
]
])
]);

PHP how to create JSON with localization arguments for APNS

I want to send JSON in an APNS with the following:
{
"aps" : {
"alert" : {
"loc-key" : "GAME_PLAY_REQUEST_FORMAT",
"loc-args" : [ "Jenna", "Frank"]
},
"sound" : "default"
},
}
Can anyone explaine how I can create this in PHP?
I have the following for JSON without the key/args:
$body['aps'] = array(
'alert ' => 'This is my messsage',
'sound' => 'default'
);
$payload = json_encode($body);
I have tried to replace the 'This is my message' with an array for loc-key and loc-args but that does not work. Also jus putting in the data as string does not work..
Hope someone can help me. I have tried multiple options and variations but nothing works..
$body = array(
"aps" => array(
"alert" => array(
"loc-key" => "GAME_PLAY_REQUEST_FORMAT",
"loc-args" => array( "Jenna", "Frank" )
),
"sound" => "default",
),
);
echo json_encode($body);
$body['aps']['alert'] = array(
"loc-key" => "GAME_PLAY_REQUEST_FORMAT",
"loc-args" => array("Jenna", "Frank")
);
just replace the content

Categories