I have 2 problems with programming telegram bot by PHP.
- problem: please, When I try send text with more lines by using API of telegram. By this code:
<?php $update = file_get_contents('php://input'); $update = json_decode($update, true); $chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null; $mess= "here is my text."; $mess = $mess. "n"; $mess = $mess. " this is new line".; send($mess, $chatId); function send($text,$chat){ if(strpos($text, "n")){ $text = urlencode($text); } $parameters = array( "chat_id" => $chat, "text" => $text, "parse_mode" => "Markdown" ); api("sendMessage?", $parameters) } function api($method,$command){ $token = "******"; $api = "https://api.telegram.org/bot$token/$method"; if(!$curld = curl_init()){ echo $curld; } curl_setopt($curld, CURLOPT_VERBOSE, true); curl_setopt($curld, CURLOPT_POST, true); curl_setopt($curld, CURLOPT_POSTFIELDS, $command); curl_setopt($curld, CURLOPT_URL, $api); curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curld, CURLOPT_TIMEOUT, 30); curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE); $apiRequest = curl_exec($curld); curl_close($curld); return $apiRequest; }
My text in telegram bot look as:
“here+is+my+text.+this+is+new+line.”
- problem, maybe question: When user come to my telegram bot, so I want to user see keyboard’s buttons, which I created. Now this I have only for new user, he must click on button “Start”. But I want to user see keyboard’s buttons, when he return sometimes.
Can you help me with this?
Thank you
Advertisement
Answer
The special chars in your text are caused by the urlencode()
call.
Since you’re passing the data as POSTFIELD, there’s no need to use urlencode()
Also, there were some syntax error’s like a extra .
behind $mess = $mess. " this is new line".;
.
Updated script:
<?php $update = file_get_contents('php://input'); $update = json_decode($update, true); $chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null; $mess= "here is my text."; $mess = $mess. "n"; $mess = $mess. " this is new line"; send($mess, $chatId); function send($text,$chat){ $parameters = array( "chat_id" => $chat, "text" => $text, "parse_mode" => "Markdown" ); api("sendMessage", $parameters); } function api($method, $command){ $token = "!!"; $api = "https://api.telegram.org/bot{$token}/{$method}"; if(!$curld = curl_init()){ echo $curld; } // curl_setopt($curld, CURLOPT_VERBOSE, true); curl_setopt($curld, CURLOPT_POST, true); curl_setopt($curld, CURLOPT_POSTFIELDS, $command); curl_setopt($curld, CURLOPT_URL, $api); curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curld, CURLOPT_TIMEOUT, 30); curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE); $apiRequest = curl_exec($curld); curl_close($curld); return $apiRequest; }