I can't find an example of sending message by telegram protocol from php. Can you give me any functional examples?
Asked
Active
Viewed 1.1e+01k times
27
-
Did you find any solution? – Vahid Najafi Jan 06 '17 at 14:02
-
Check out [my answer](https://stackoverflow.com/a/57650422/5407848) – Accountant م Aug 26 '19 at 21:08
3 Answers
36
I use the following function:
function sendMessage($chatID, $messaggio, $token) {
echo "sending message to " . $chatID . "\n";
$url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
$url = $url . "&text=" . urlencode($messaggio);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
and you call in this way
$token = "<insert bot token here>";
$chatid = "<chatID>";
sendMessage($chatid, "Hello World", $token);

akkez
- 13
- 3

Mauro Delrio
- 1,941
- 1
- 12
- 12
-
4
-
it seems many of telegram developers are Iranian?. but i seeking for simple samples about bots ... all the libraries are complex in c# and php. is there any bank of codes for php develoers? or any training or tutorial? – saber tabatabaee yazdi Jun 16 '17 at 08:41
-
can you add to your answer how to parse the response. {"ok":false,"error_code":400,"description":"Bad Request: chat not found"} – hamish Apr 27 '19 at 01:30
15
Simple way:
$token = "YOUR_BOT's_TOKEN";
$data = [
'text' => 'your message here',
'chat_id' => 'the_chat_id_here'
];
file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );

Babak Bandpey
- 900
- 1
- 12
- 20
5
Well seems it is a quite old post, but well no answers, so I hope it helps someone. You could have used example from the following repository Telegram Bot Client in PHP that I am currently developing. This is the method I used to send message.
// initialise variables here
$chat_id = 1231231231;
// path to the picture,
$text = 'your text goes here';
// following ones are optional, so could be set as null
$disable_web_page_preview = null;
$reply_to_message_id = null;
$reply_markup = null;
$data = array(
'chat_id' => urlencode($chat_id),
'text' => urlencode($text),
'disable_web_page_preview' => urlencode($disable_web_page_preview),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$url = https://api.telegram.org/botYOUR_TOKEN_GOES_HERE/sendMessage;
// open connection
$ch = curl_init();
// set the url
curl_setopt($ch, CURLOPT_URL, $url);
// number of POST vars
curl_setopt($ch, CURLOPT_POST, count($data));
// POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// To display result of curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
-
use https://github.com/php-telegram-bot/core this official library using composer. And simply use sendMessage function should be the shortest solution – Dilushan Sep 12 '19 at 09:49