Skip to content

Commit 70e0155

Browse files
committed
The simplest telegram php bot with Curl sending
The simplest telegram php bot with Curl sending functions Curl sendPhoto, sendLocation, sendMessage etc...
1 parent d96026c commit 70e0155

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

curl-funtions.php

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Telegram Bot Debugger. Telegram curl post message.
4+
*
5+
* Edit by 4eburashk http://csn.net4me.net
6+
* On Sat 22 Jan 2022 07:15:17 PM MSK
7+
*
8+
* version: 2.1
9+
* release: 2022-01-23
10+
*/
11+
$access_token = '12345678:AAF-XXXXXXXXXXXXXXXXXX';
12+
$api = 'https://api.telegram.org/bot' . $access_token;
13+
/**
14+
* Задаём основные переменные.
15+
*/
16+
$output = json_decode(file_get_contents('php://input'), TRUE);
17+
@$chat_id = $output['message']['chat']['id'];
18+
@$message = $output['message']['text'];
19+
@$from = $output['message']['from'];
20+
@$names = $from['id']." ".$from['first_name']." ".$from['username'];
21+
@$callback_query = $output['callback_query'];
22+
@$data = $callback_query['data'];
23+
@$chat_id_in = $callback_query['message']['chat']['id'];
24+
@$message_id = $callback_query['message']['message_id'];
25+
// Инлайн клавиатура:
26+
$inline_button3 = array("inline_message_id"=>"3","text"=>"send pic","callback_data"=>'/3');
27+
$inline_button4 = array("inline_message_id"=>"4","text"=>"send location","callback_data"=>'/4');
28+
$inline_button5 = array("inline_message_id"=>"5","text"=>"show all","callback_data"=>'/1');
29+
$inline_button6 = array("inline_message_id"=>"6","text"=>"show callback","callback_data"=>'/2');
30+
$inline_keyboard1 = [[$inline_button3,$inline_button4],[$inline_button5,$inline_button6]];
31+
$keyboard1=array("inline_keyboard"=>$inline_keyboard1);
32+
$replyMarkup1 = json_encode($keyboard1);
33+
// Ожидаем сообщения:
34+
switch($message) {
35+
case '/start':
36+
sendMessage($chat_id, "\xF0\x9F\x93\xA1 бот debugger на связи!");
37+
break;
38+
default:
39+
$myDebug = "<pre>". json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).PHP_EOL."</pre>";
40+
sendMessage($chat_id, $myDebug, 0, 'html', $replyMarkup1);
41+
break;
42+
}
43+
44+
// callback data Реакции на нажатие кнопок.
45+
switch($data){
46+
case '/1':
47+
send_answerCallbackQuery($callback_query['id'], null, false); // убираем ожидание (часы на кнопке)
48+
$myDebug = "<pre>".json_encode($output)."</pre>";
49+
sendMessage($chat_id_in, $myDebug, 0, 'html');
50+
break;
51+
case '/2':
52+
send_answerCallbackQuery($callback_query['id'],'callback id: '.$callback_query['id'], true); // c алертом
53+
$myDebug = "<pre>". json_encode($output) ."</pre>";
54+
sendMessage($chat_id_in, $myDebug, 0, 'html', $replyMarkup1);
55+
break;
56+
case '/3':
57+
send_answerCallbackQuery($callback_query['id'], null, false); // убираем ожидание (часы на кнопке)
58+
$text = "Отправка *картинки* _с текстом_, разметка текста в caption не рабоает. Только текст.";
59+
sendPhoto($chat_id_in, $text, '../images/icon-256x256.png', 0);
60+
break;
61+
case '/4':
62+
send_answerCallbackQuery($callback_query['id'], null, false); // убираем ожидание (часы на кнопке)
63+
// Отправка координаты. К сожалению (пока?), без текста и возможности подписи.
64+
sendLocation($chat_id_in, '55.85303647558608', '37.61637715031881');
65+
break;
66+
}
67+
68+
exit(0);
69+
//***************** functions ******************
70+
71+
// Отправка сообщения или сообщения с клавиатурой. Возвращает id отправленного сообщения.
72+
function sendMessage($chat_id, $message, $mute=false, $pmode='HTML', $replyMarkup=false){
73+
$url = $GLOBALS['api'] . "/sendMessage";
74+
$post_fields = array(
75+
'chat_id' => $chat_id,
76+
'text' => $message,
77+
'disable_notification' => $mute,
78+
'parse_mode'=> $pmode,
79+
'reply_markup'=>$replyMarkup
80+
);
81+
$ch = curl_init();
82+
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
83+
curl_setopt($ch, CURLOPT_URL, $url);
84+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
85+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
86+
$response = curl_exec($ch);
87+
curl_close($ch);
88+
usleep(500);
89+
$res = json_decode($response, TRUE);
90+
return isset($res['result'])?$res['result']['message_id']:0; //int id сообщения.
91+
}
92+
93+
// Ответ на нажатие кнопок
94+
function send_answerCallbackQuery($callback_query_id, $text='', $show_alert=false){
95+
@file_get_contents($GLOBALS['api'] . '/answerCallbackQuery?callback_query_id=' . $callback_query_id . '&text=' . $text . '&show_alert=' . $show_alert );
96+
}
97+
98+
// отправка location в telegram:
99+
function sendLocation($chat_id, $latitude='55.75236229347181', $longitude='37.66827791767219', $mute=false){
100+
$url = $GLOBALS['api'] . "/sendLocation";
101+
$post_fields = array(
102+
'chat_id' => $chat_id,
103+
'latitude' => $latitude,
104+
'longitude' => $longitude,
105+
'disable_notification' => $mute,
106+
);
107+
$ch = curl_init();
108+
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
109+
curl_setopt($ch, CURLOPT_URL, $url);
110+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
111+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
112+
$res = curl_exec($ch);
113+
return $res;
114+
}
115+
116+
// отправка сообщения с картинкой в telegram:
117+
function sendPhoto($chat_id, $caption, $img, $mute=false){
118+
$url = $GLOBALS['api'] . "/sendPhoto";
119+
// в картинках, парсмод только html! проверено.
120+
$post_fields = array(
121+
'chat_id' => $chat_id,
122+
'disable_notification' => $mute,
123+
'caption' => htmlspecialchars($caption,ENT_QUOTES),
124+
'photo' => new CURLFile(realpath($img))
125+
);
126+
$ch = curl_init();
127+
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
128+
curl_setopt($ch, CURLOPT_URL, $url);
129+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
130+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
131+
$res = curl_exec($ch);
132+
return $res;
133+
}
134+
135+
// удаление сообщения из tg:
136+
function deleteMessage($cid, $mid){
137+
$dresponse = @file_get_contents($GLOBALS['api'] . '/deleteMessage?chat_id=' . $cid . '&message_id=' . $mid);
138+
return $dresponse;
139+
}
140+
141+
// END

0 commit comments

Comments
 (0)