api.telegram.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. /**
  3. * Telegram bot API implementation
  4. */
  5. class UbillingTelegram {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains current instance bot token
  14. *
  15. * @var string
  16. */
  17. protected $botToken = '';
  18. /**
  19. * Default debug flag wich enables telegram replies display
  20. *
  21. * @var bool
  22. */
  23. protected $debug = false;
  24. /**
  25. * Contains base Telegram API URL
  26. */
  27. protected $apiUrl = 'https://api.telegram.org/bot';
  28. /**
  29. * Contains telegram messages path
  30. */
  31. const QUEUE_PATH = 'content/telegram/';
  32. /**
  33. * Maximum message length
  34. */
  35. const MESSAGE_LIMIT = 4095;
  36. /**
  37. * Creates new Telegram object instance
  38. *
  39. * @param string $token
  40. */
  41. public function __construct($token = '') {
  42. if (!empty($token)) {
  43. $this->botToken = $token;
  44. }
  45. $this->loadAlter();
  46. $this->setOptions();
  47. }
  48. /**
  49. * Sets current instance auth token
  50. *
  51. * @param string $token
  52. *
  53. * @return void
  54. */
  55. public function setToken($token) {
  56. $this->botToken = $token;
  57. }
  58. /**
  59. * Object instance debug state setter
  60. *
  61. * @param bool $state
  62. *
  63. * @return void
  64. */
  65. public function setDebug($state) {
  66. $this->debug = $state;
  67. }
  68. /**
  69. * Loads system alter config into protected property for further usage
  70. *
  71. * @global object $ubillingConfig
  72. *
  73. * @return void
  74. */
  75. protected function loadAlter() {
  76. global $ubillingConfig;
  77. $this->altCfg = $ubillingConfig->getAlter();
  78. }
  79. /**
  80. * Sets some current instance options if required
  81. *
  82. * @return void
  83. */
  84. protected function setOptions() {
  85. //settin debug flag
  86. if (isset($this->altCfg['TELEGRAM_DEBUG'])) {
  87. if ($this->altCfg['TELEGRAM_DEBUG']) {
  88. $this->debug = true;
  89. }
  90. }
  91. if (isset($this->altCfg['TELEGRAM_API_URL'])) {
  92. if (!empty($this->altCfg['TELEGRAM_API_URL'])) {
  93. $this->setApiUrl($this->altCfg['TELEGRAM_API_URL']);
  94. }
  95. }
  96. }
  97. /**
  98. * Setter of custom API URL (legacy fallback)
  99. *
  100. * @param string $url
  101. *
  102. * @return void
  103. */
  104. protected function setApiUrl($url) {
  105. $this->apiUrl = $url;
  106. }
  107. /**
  108. * Stores message in telegram sending queue. Use this method in your modules.
  109. *
  110. * @param int $chatid
  111. * @param string $message
  112. * @param bool $translit
  113. * @param string $module
  114. *
  115. * @return bool
  116. */
  117. public function sendMessage($chatid, $message, $translit = false, $module = '') {
  118. $result = false;
  119. $chatid = trim($chatid);
  120. $module = (!empty($module)) ? ' MODULE ' . $module : '';
  121. $prefix = 'tlg_';
  122. if (!empty($chatid)) {
  123. $message = str_replace(array("\n\r", "\n", "\r"), ' ', $message);
  124. if ($translit) {
  125. $message = zb_TranslitString($message);
  126. }
  127. $message = trim($message);
  128. $queueId = time();
  129. $offset = 0;
  130. $filename = self::QUEUE_PATH . $prefix . $queueId . '_' . $offset;
  131. if (file_exists($filename)) {
  132. while (file_exists($filename)) {
  133. $offset++; //incremeting number of messages per second
  134. $filename = self::QUEUE_PATH . $prefix . $queueId . '_' . $offset;
  135. }
  136. }
  137. $storedata = 'CHATID="' . $chatid . '"' . "\n";
  138. $storedata .= 'MESSAGE="' . $message . '"' . "\n";
  139. file_put_contents($filename, $storedata);
  140. log_register('UTLG SEND MESSAGE FOR `' . $chatid . '` AS `' . $prefix . $queueId . '_' . $offset . '` ' . $module);
  141. $result = true;
  142. }
  143. return ($result);
  144. }
  145. /**
  146. * Returns count of messages available in queue
  147. *
  148. * @return int
  149. */
  150. public function getQueueCount() {
  151. $messagesQueueCount = rcms_scandir(self::QUEUE_PATH);
  152. $result = sizeof($messagesQueueCount);
  153. return ($result);
  154. }
  155. /**
  156. * Returns array containing all messages queue data as index=>data
  157. *
  158. * @return array
  159. */
  160. public function getQueueData() {
  161. $result = array();
  162. $messagesQueue = rcms_scandir(self::QUEUE_PATH);
  163. if (!empty($messagesQueue)) {
  164. foreach ($messagesQueue as $io => $eachmessage) {
  165. $messageDate = date("Y-m-d H:i:s", filectime(self::QUEUE_PATH . $eachmessage));
  166. $messageData = rcms_parse_ini_file(self::QUEUE_PATH . $eachmessage);
  167. $result[$io]['filename'] = $eachmessage;
  168. $result[$io]['date'] = $messageDate;
  169. $result[$io]['chatid'] = $messageData['CHATID'];
  170. $result[$io]['message'] = $messageData['MESSAGE'];
  171. }
  172. }
  173. return ($result);
  174. }
  175. /**
  176. * Deletes message from local queue
  177. *
  178. * @param string $filename Existing message filename
  179. *
  180. * @return int 0 - ok, 1 - deletion unsuccessful, 2 - file not found
  181. */
  182. public function deleteMessage($filename) {
  183. if (file_exists(self::QUEUE_PATH . $filename)) {
  184. unlink(self::QUEUE_PATH . $filename);
  185. $result = 0;
  186. if (file_exists(self::QUEUE_PATH . $filename)) {
  187. $result = 1;
  188. }
  189. } else {
  190. $result = 2;
  191. }
  192. return ($result);
  193. }
  194. /**
  195. * Returns raw updates array
  196. *
  197. * @param int $offset
  198. * @param int $limit
  199. * @param int $timeout
  200. *
  201. * @return array
  202. *
  203. * @throws Exception
  204. */
  205. protected function getUpdatesRaw($offset = '', $limit = '', $timeout = '') {
  206. $result = array();
  207. $timeout = vf($timeout, 3);
  208. $limit = vf($limit, 3);
  209. $offset = mysql_real_escape_string($offset);
  210. $timeout = (!empty($timeout)) ? $timeout : 0; //default timeout in seconds is 0
  211. $limit = (!empty($limit)) ? $limit : 100; //defult limit is 100
  212. /**
  213. * Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates.
  214. * By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is
  215. * called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from
  216. * the end of the updates queue. All previous updates will forgotten.
  217. */
  218. $offset = (!empty($offset)) ? '&offset=' . $offset : '';
  219. if (!empty($this->botToken)) {
  220. $options = '?timeout=' . $timeout . '&limit=' . $limit . $offset;
  221. $url = $this->apiUrl . $this->botToken . '/getUpdates' . $options;
  222. $ch = curl_init();
  223. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  224. curl_setopt($ch, CURLOPT_URL, $url);
  225. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  226. curl_setopt($ch, CURLOPT_POST, 1);
  227. @$reply = curl_exec($ch);
  228. if ($this->debug) {
  229. $curlError = curl_error($ch);
  230. show_info($url);
  231. if (!empty($curlError)) {
  232. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  233. } else {
  234. show_success(__('Telegram API connection to') . ' ' . $this->apiUrl . ' ' . __('success'));
  235. }
  236. }
  237. curl_close($ch);
  238. if (!empty($reply)) {
  239. $result = json_decode($reply, true);
  240. }
  241. if ($this->debug) {
  242. debarr($result);
  243. }
  244. } else {
  245. throw new Exception('EX_TOKEN_EMPTY');
  246. }
  247. return ($result);
  248. }
  249. /**
  250. * Returns all messages received by bot
  251. *
  252. * @return array
  253. * @throws Exception
  254. */
  255. public function getBotChats() {
  256. $result = array();
  257. if (!empty($this->botToken)) {
  258. $rawUpdates = $this->getUpdatesRaw();
  259. if (!empty($rawUpdates)) {
  260. if (isset($rawUpdates['result'])) {
  261. $allUpdates = $rawUpdates['result'];
  262. foreach ($allUpdates as $io => $each) {
  263. if (isset($each['message'])) {
  264. if (isset($each['message']['chat'])) {
  265. if (isset($each['message']['chat']['type'])) {
  266. $messageData = $each['message'];
  267. if ($messageData['chat']['type'] == 'private') {
  268. //direct message
  269. if (isset($messageData['message_id'])) {
  270. $messageId = $messageData['message_id'];
  271. $result[$messageId]['id'] = $messageId;
  272. $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']);
  273. $result[$messageId]['chatid'] = $messageData['from']['id'];
  274. $result[$messageId]['from'] = @$messageData['from']['username'];
  275. $result[$messageId]['text'] = @$messageData['text'];
  276. $result[$messageId]['type'] = 'user';
  277. $result[$messageId]['chanid'] = '';
  278. $result[$messageId]['channame'] = '';
  279. $result[$messageId]['updateid'] = @$each['update_id'];
  280. }
  281. }
  282. //supergroup message
  283. if ($messageData['chat']['type'] == 'supergroup') {
  284. if (isset($messageData['message_id'])) {
  285. $messageId = $messageData['message_id'];
  286. $result[$messageId]['id'] = $messageId;
  287. $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']);
  288. $result[$messageId]['chatid'] = $messageData['from']['id'];
  289. $result[$messageId]['from'] = @$messageData['from']['username'];
  290. $result[$messageId]['text'] = @$messageData['text'];
  291. $result[$messageId]['type'] = 'supergroup';
  292. $result[$messageId]['chanid'] = $messageData['chat']['id'];
  293. $result[$messageId]['channame'] = $messageData['chat']['username'];
  294. $result[$messageId]['updateid'] = '';
  295. $result[$messageId]['updateid'] = @$each['update_id'];
  296. }
  297. }
  298. }
  299. }
  300. }
  301. //channel message
  302. if (isset($each['channel_post'])) {
  303. $messageData = $each['channel_post'];
  304. if (isset($messageData['message_id'])) {
  305. $messageId = $messageData['message_id'];
  306. $result[$messageId]['id'] = $messageId;
  307. $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']);
  308. $result[$messageId]['chatid'] = $messageData['chat']['id'];
  309. $result[$messageId]['from'] = @$messageData['chat']['username'];
  310. $result[$messageId]['text'] = @$messageData['text'];
  311. $result[$messageId]['type'] = 'channel';
  312. }
  313. }
  314. }
  315. }
  316. }
  317. } else {
  318. throw new Exception('EX_TOKEN_EMPTY');
  319. }
  320. return ($result);
  321. }
  322. /**
  323. * Returns current bot contacts list as chat_id=>name
  324. *
  325. * @return array
  326. */
  327. public function getBotContacts() {
  328. $result = array();
  329. $updatesRaw = $this->getUpdatesRaw();
  330. if (!empty($updatesRaw)) {
  331. if (isset($updatesRaw['result'])) {
  332. if (!empty($updatesRaw['result'])) {
  333. foreach ($updatesRaw['result'] as $io => $each) {
  334. //supergroup messages
  335. if (isset($each['message'])) {
  336. if (isset($each['message']['chat'])) {
  337. if (isset($each['message']['chat']['type'])) {
  338. if ($each['message']['chat']['type'] = 'supergroup') {
  339. $groupData = $each['message']['chat'];
  340. $result[$groupData['id']]['chatid'] = $groupData['id'];
  341. $groupName = (!empty($groupData['username'])) ? $groupData['username'] : @$groupData['title']; //only title for private groups
  342. $result[$groupData['id']]['name'] = $groupName;
  343. $result[$groupData['id']]['first_name'] = @$groupData['title'];
  344. $result[$groupData['id']]['last_name'] = '';
  345. $result[$groupData['id']]['type'] = 'supergroup';
  346. $result[$groupData['id']]['lastmessage'] = strip_tags(@$each['message']['text']);
  347. }
  348. }
  349. }
  350. }
  351. //direct user message
  352. if (isset($each['message'])) {
  353. if (isset($each['message']['from'])) {
  354. if (isset($each['message']['from']['id'])) {
  355. $messageData = $each['message']['from'];
  356. $result[$messageData['id']]['chatid'] = $messageData['id'];
  357. $result[$messageData['id']]['name'] = @$messageData['username']; //may be empty
  358. $result[$messageData['id']]['first_name'] = @$messageData['first_name'];
  359. $result[$messageData['id']]['last_name'] = @$messageData['last_name'];
  360. $result[$messageData['id']]['type'] = 'user';
  361. $result[$messageData['id']]['lastmessage'] = strip_tags(@$each['message']['text']);
  362. }
  363. }
  364. }
  365. //channel message
  366. if (isset($each['channel_post'])) {
  367. if (isset($each['channel_post']['chat'])) {
  368. if (isset($each['channel_post']['chat']['id'])) {
  369. $chatData = $each['channel_post']['chat'];
  370. $result[$chatData['id']]['chatid'] = $chatData['id'];
  371. $result[$chatData['id']]['name'] = $chatData['username'];
  372. $result[$chatData['id']]['first_name'] = '';
  373. $result[$chatData['id']]['last_name'] = '';
  374. $result[$chatData['id']]['type'] = 'channel';
  375. $result[$messageData['id']]['lastmessage'] = strip_tags(@$each['message']['text']);
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. return ($result);
  384. }
  385. /**
  386. * Preprocess keyboard for sending with directPushMessage
  387. *
  388. * @param array $buttonsArray
  389. * @param bool $inline
  390. * @param bool $resize
  391. * @param bool $oneTime
  392. *
  393. * @return array
  394. */
  395. public function makeKeyboard($buttonsArray, $inline = false, $resize = true, $oneTime = false) {
  396. $result = array();
  397. if (!empty($buttonsArray)) {
  398. if (!$inline) {
  399. $result['type'] = 'keyboard';
  400. $keyboardMarkup = array(
  401. 'keyboard' => $buttonsArray,
  402. 'resize_keyboard' => $resize,
  403. 'one_time_keyboard' => $oneTime
  404. );
  405. $result['markup'] = $keyboardMarkup;
  406. }
  407. if ($inline) {
  408. $result['type'] = 'inline';
  409. $keyboardMarkup = $buttonsArray;
  410. $result['markup'] = $keyboardMarkup;
  411. }
  412. }
  413. return ($result);
  414. }
  415. /**
  416. * Split message into chunks of safe size
  417. *
  418. * @param string $message
  419. *
  420. * @return array
  421. */
  422. protected function splitMessage($message) {
  423. $result = preg_split('~~u', $message, -1, PREG_SPLIT_NO_EMPTY);
  424. $chunks = array_chunk($result, self::MESSAGE_LIMIT);
  425. foreach ($chunks as $i => $chunk) {
  426. $chunks[$i] = join('', (array) $chunk);
  427. }
  428. $result = $chunks;
  429. return ($result);
  430. }
  431. /**
  432. * Sends message to some chat id using Telegram API
  433. *
  434. * @param int $chatid remote chatId
  435. * @param string $message text message to send
  436. * @param array $keyboard keyboard encoded with makeKeyboard method
  437. * @param bool $nosplit dont automatically split message into 4096 slices
  438. * @param int $replyToMsgId optional message ID which is reply for
  439. *
  440. * @return string/bool
  441. */
  442. public function directPushMessage($chatid, $message, $keyboard = array(), $noSplit = false, $replyToMsgId = '') {
  443. $result = '';
  444. if ($noSplit) {
  445. $result = $this->apiSendMessage($chatid, $message, $keyboard, $replyToMsgId);
  446. } else {
  447. $messageSize = mb_strlen($message, 'UTF-8');
  448. if ($messageSize > self::MESSAGE_LIMIT) {
  449. $messageSplit = $this->splitMessage($message);
  450. if (!empty($messageSplit)) {
  451. foreach ($messageSplit as $io => $eachMessagePart) {
  452. $result = $this->apiSendMessage($chatid, $eachMessagePart, $keyboard, $replyToMsgId);
  453. }
  454. }
  455. } else {
  456. $result = $this->apiSendMessage($chatid, $message, $keyboard, $replyToMsgId);
  457. }
  458. }
  459. return ($result);
  460. }
  461. /**
  462. * Sends message to some chat id via Telegram API
  463. *
  464. * @param int $chatid remote chatId
  465. * @param string $message text message to send
  466. * @param array $keyboard keyboard encoded with makeKeyboard method
  467. * @param int $replyToMsgId optional message ID which is reply for
  468. * @throws Exception
  469. *
  470. * @return string/bool
  471. */
  472. protected function apiSendMessage($chatid, $message, $keyboard = array(), $replyToMsgId = '') {
  473. $result = '';
  474. $data['chat_id'] = $chatid;
  475. $data['text'] = $message;
  476. if ($this->debug) {
  477. debarr($data);
  478. }
  479. //default sending method
  480. $method = 'sendMessage';
  481. //setting optional replied message ID for normal messages
  482. if ($replyToMsgId) {
  483. $method = 'sendMessage?reply_to_message_id=' . $replyToMsgId;
  484. }
  485. //location sending
  486. if (ispos($message, 'sendLocation:')) {
  487. $cleanGeo = str_replace('sendLocation:', '', $message);
  488. $cleanGeo = explode(',', $cleanGeo);
  489. $geoLat = trim($cleanGeo[0]);
  490. $geoLon = trim($cleanGeo[1]);
  491. $locationParams = '?chat_id=' . $chatid . '&latitude=' . $geoLat . '&longitude=' . $geoLon;
  492. if ($replyToMsgId) {
  493. $locationParams .= '&reply_to_message_id=' . $replyToMsgId;
  494. }
  495. $method = 'sendLocation' . $locationParams;
  496. }
  497. //custom markdown
  498. if (ispos($message, 'parseMode:{')) {
  499. if (preg_match('!\{(.*?)\}!si', $message, $tmpMode)) {
  500. $cleanParseMode = $tmpMode[1];
  501. $parseModeMask = 'parseMode:{' . $cleanParseMode . '}';
  502. $cleanMessage = str_replace($parseModeMask, '', $message);
  503. $data['text'] = $cleanMessage;
  504. $method = 'sendMessage?parse_mode=' . $cleanParseMode;
  505. if ($replyToMsgId) {
  506. $method .= '&reply_to_message_id=' . $replyToMsgId;
  507. }
  508. }
  509. }
  510. //venue sending
  511. if (ispos($message, 'sendVenue:')) {
  512. if (preg_match('!\[(.*?)\]!si', $message, $tmpGeo)) {
  513. $cleanGeo = $tmpGeo[1];
  514. }
  515. if (preg_match('!\((.*?)\)!si', $message, $tmpAddr)) {
  516. $cleanAddr = $tmpAddr[1];
  517. }
  518. if (preg_match('!\{(.*?)\}!si', $message, $tmpTitle)) {
  519. $cleanTitle = $tmpTitle[1];
  520. }
  521. $data['title'] = $cleanTitle;
  522. $data['address'] = $cleanAddr;
  523. $cleanGeo = explode(',', $cleanGeo);
  524. $geoLat = trim($cleanGeo[0]);
  525. $geoLon = trim($cleanGeo[1]);
  526. $locationParams = '?chat_id=' . $chatid . '&latitude=' . $geoLat . '&longitude=' . $geoLon;
  527. if ($replyToMsgId) {
  528. $locationParams .= '&reply_to_message_id=' . $replyToMsgId;
  529. }
  530. $method = 'sendVenue' . $locationParams;
  531. }
  532. //photo sending
  533. if (ispos($message, 'sendPhoto:')) {
  534. if (preg_match('!\[(.*?)\]!si', $message, $tmpPhoto)) {
  535. $cleanPhoto = $tmpPhoto[1];
  536. }
  537. if (preg_match('!\{(.*?)\}!si', $message, $tmpCaption)) {
  538. $cleanCaption = $tmpCaption[1];
  539. $cleanCaption = urlencode($cleanCaption);
  540. }
  541. $photoParams = '?chat_id=' . $chatid . '&photo=' . $cleanPhoto;
  542. if (!empty($cleanCaption)) {
  543. $photoParams .= '&caption=' . $cleanCaption;
  544. }
  545. if ($replyToMsgId) {
  546. $photoParams .= '&reply_to_message_id=' . $replyToMsgId;
  547. }
  548. $method = 'sendPhoto' . $photoParams;
  549. }
  550. //sending keyboard
  551. if (!empty($keyboard)) {
  552. if (isset($keyboard['type'])) {
  553. if ($keyboard['type'] == 'keyboard') {
  554. $encodedKeyboard = json_encode($keyboard['markup']);
  555. $data['reply_markup'] = $encodedKeyboard;
  556. }
  557. if ($keyboard['type'] == 'inline') {
  558. $encodedKeyboard = json_encode(array('inline_keyboard' => $keyboard['markup']));
  559. $data['reply_markup'] = $encodedKeyboard;
  560. $data['parse_mode'] = 'HTML';
  561. }
  562. $method = 'sendMessage';
  563. }
  564. }
  565. //removing keyboard
  566. if (ispos($message, 'removeKeyboard:')) {
  567. $keybRemove = array(
  568. 'remove_keyboard' => true
  569. );
  570. $encodedMarkup = json_encode($keybRemove);
  571. $cleanMessage = str_replace('removeKeyboard:', '', $message);
  572. if (empty($cleanMessage)) {
  573. $cleanMessage = __('Keyboard deleted');
  574. }
  575. $data['text'] = $cleanMessage;
  576. $data['reply_markup'] = $encodedMarkup;
  577. }
  578. //banChatMember
  579. if (ispos($message, 'banChatMember:')) {
  580. if (preg_match('!\[(.*?)\]!si', $message, $tmpBanString)) {
  581. $cleanBanString = explode('@', $tmpBanString[1]);
  582. $banUserId = $cleanBanString[0];
  583. $banChatId = $cleanBanString[1];
  584. }
  585. $banParams = '?chat_id=' . $banChatId . '&user_id=' . $banUserId;
  586. $method = 'banChatMember' . $banParams;
  587. }
  588. //unbanChatMember
  589. if (ispos($message, 'unbanChatMember:')) {
  590. if (preg_match('!\[(.*?)\]!si', $message, $tmpUnbanString)) {
  591. $cleanUnbanString = explode('@', $tmpUnbanString[1]);
  592. $unbanUserId = $cleanUnbanString[0];
  593. $unbanChatId = $cleanUnbanString[1];
  594. }
  595. $unbanParams = '?chat_id=' . $unbanChatId . '&user_id=' . $unbanUserId;
  596. $method = 'unbanChatMember' . $unbanParams;
  597. }
  598. //deleting message by its id
  599. if (ispos($message, 'removeChatMessage:')) {
  600. if (preg_match('!\[(.*?)\]!si', $message, $tmpRemoveString)) {
  601. $cleanRemoveString = explode('@', $tmpRemoveString[1]);
  602. $removeMessageId = $cleanRemoveString[0];
  603. $removeChatId = $cleanRemoveString[1];
  604. $removeParams = '?chat_id=' . $removeChatId . '&message_id=' . $removeMessageId;
  605. $method = 'deleteMessage' . $removeParams;
  606. }
  607. }
  608. //POST data encoding
  609. $data_json = json_encode($data);
  610. if (!empty($this->botToken)) {
  611. $url = $this->apiUrl . $this->botToken . '/' . $method;
  612. if ($this->debug) {
  613. deb($url);
  614. }
  615. $ch = curl_init();
  616. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  617. curl_setopt($ch, CURLOPT_URL, $url);
  618. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  619. curl_setopt($ch, CURLOPT_POST, 1);
  620. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  621. if ($this->debug) {
  622. $result = curl_exec($ch);
  623. deb($result);
  624. $curlError = curl_error($ch);
  625. if (!empty($curlError)) {
  626. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  627. } else {
  628. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  629. }
  630. } else {
  631. $result = curl_exec($ch);
  632. }
  633. curl_close($ch);
  634. } else {
  635. throw new Exception('EX_TOKEN_EMPTY');
  636. }
  637. return ($result);
  638. }
  639. /**
  640. * Sets HTTPS web hook URL for some bot
  641. *
  642. * @param string $webHookUrl HTTPS url to send updates to. Use an empty string to remove webhook integration
  643. * @param int $maxConnections Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
  644. * @param array $allowedUpdates Array of updates types allowed for that hook. Example: array('update_id', 'message', 'chat_member', 'message_reaction')
  645. * some of this types https://core.telegram.org/bots/api#update or leave this empty in most cases
  646. * @return string
  647. */
  648. public function setWebHook($webHookUrl, $maxConnections = 40, $allowedUpdates = array()) {
  649. $result = '';
  650. if (!empty($this->botToken)) {
  651. $data = array();
  652. if (!empty($webHookUrl)) {
  653. $method = 'setWebhook';
  654. if (ispos($webHookUrl, 'https://')) {
  655. $data['url'] = $webHookUrl;
  656. $data['max_connections'] = $maxConnections;
  657. if (!empty($allowedUpdates)) {
  658. $data['allowed_updates'] = $allowedUpdates;
  659. }
  660. } else {
  661. throw new Exception('EX_NOT_SSL_URL');
  662. }
  663. } else {
  664. $method = 'deleteWebhook';
  665. }
  666. $url = $this->apiUrl . $this->botToken . '/' . $method;
  667. if ($this->debug) {
  668. deb($url);
  669. }
  670. $ch = curl_init();
  671. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  672. curl_setopt($ch, CURLOPT_URL, $url);
  673. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  674. curl_setopt($ch, CURLOPT_POST, 1);
  675. if (!empty($data)) {
  676. $data_json = json_encode($data);
  677. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  678. }
  679. if ($this->debug) {
  680. $result = curl_exec($ch);
  681. deb($result);
  682. $curlError = curl_error($ch);
  683. if (!empty($curlError)) {
  684. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  685. } else {
  686. show_success(__('Telegram API Hook') . ' ' . $this->apiUrl . ' =>' . print_r($data, true) . '|' . $data_json . __('success'));
  687. show_success($url);
  688. }
  689. } else {
  690. $result = curl_exec($ch);
  691. }
  692. curl_close($ch);
  693. } else {
  694. throw new Exception('EX_TOKEN_EMPTY');
  695. }
  696. return ($result);
  697. }
  698. /**
  699. * Returns bot web hook info
  700. *
  701. * @return string
  702. */
  703. public function getWebHookInfo() {
  704. $result = '';
  705. if (!empty($this->botToken)) {
  706. $method = 'getWebhookInfo';
  707. $url = $this->apiUrl . $this->botToken . '/' . $method;
  708. if ($this->debug) {
  709. deb($url);
  710. }
  711. $ch = curl_init();
  712. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  713. curl_setopt($ch, CURLOPT_URL, $url);
  714. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  715. if ($this->debug) {
  716. $result = curl_exec($ch);
  717. deb($result);
  718. $curlError = curl_error($ch);
  719. if (!empty($curlError)) {
  720. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  721. } else {
  722. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  723. }
  724. } else {
  725. $result = curl_exec($ch);
  726. }
  727. curl_close($ch);
  728. }
  729. return ($result);
  730. }
  731. /**
  732. * Returns chat data array by its chatId
  733. *
  734. * @param int chatId
  735. *
  736. * @return array
  737. */
  738. public function getChatInfo($chatId) {
  739. $result = array();
  740. if (!empty($this->botToken) and (!empty($chatId))) {
  741. $method = 'getChat';
  742. $url = $this->apiUrl . $this->botToken . '/' . $method . '?chat_id=' . $chatId;
  743. if ($this->debug) {
  744. deb($url);
  745. }
  746. $ch = curl_init();
  747. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  748. curl_setopt($ch, CURLOPT_URL, $url);
  749. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  750. if ($this->debug) {
  751. $result = curl_exec($ch);
  752. deb($result);
  753. $curlError = curl_error($ch);
  754. if (!empty($curlError)) {
  755. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  756. } else {
  757. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  758. }
  759. } else {
  760. $result = curl_exec($ch);
  761. }
  762. curl_close($ch);
  763. if (!empty($result)) {
  764. $result = json_decode($result, true);
  765. }
  766. }
  767. return ($result);
  768. }
  769. /**
  770. * Returns file path by its file ID
  771. *
  772. * @param string $fileId
  773. *
  774. * @return string
  775. */
  776. public function getFilePath($fileId) {
  777. $result = '';
  778. if (!empty($this->botToken)) {
  779. $method = 'getFile';
  780. $url = $this->apiUrl . $this->botToken . '/' . $method . '?file_id=' . $fileId;
  781. if ($this->debug) {
  782. deb($url);
  783. }
  784. $ch = curl_init();
  785. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  786. curl_setopt($ch, CURLOPT_URL, $url);
  787. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  788. if ($this->debug) {
  789. $result = curl_exec($ch);
  790. deb($result);
  791. $curlError = curl_error($ch);
  792. if (!empty($curlError)) {
  793. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  794. } else {
  795. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  796. }
  797. } else {
  798. $result = curl_exec($ch);
  799. }
  800. curl_close($ch);
  801. if (!empty($result)) {
  802. $result = json_decode($result, true);
  803. if (@$result['ok']) {
  804. //we got it!
  805. $result = $result['result']['file_path'];
  806. } else {
  807. //something went wrong
  808. $result = '';
  809. }
  810. }
  811. }
  812. return ($result);
  813. }
  814. /**
  815. * Returns some file content
  816. *
  817. * @param string $filePath
  818. *
  819. * @return mixed
  820. */
  821. public function downloadFile($filePath) {
  822. $result = '';
  823. if (!empty($this->botToken)) {
  824. $cleanApiUrl = str_replace('bot', '', $this->apiUrl);
  825. $url = $cleanApiUrl . 'file/bot' . $this->botToken . '/' . $filePath;
  826. $ch = curl_init();
  827. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  828. curl_setopt($ch, CURLOPT_URL, $url);
  829. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  830. $result = curl_exec($ch);
  831. curl_close($ch);
  832. }
  833. return ($result);
  834. }
  835. /**
  836. * Returns preprocessed message in standard, fixed fields format
  837. *
  838. * @param array $messageData
  839. * @param bool $isChannel
  840. *
  841. * @return array
  842. */
  843. protected function preprocessMessageData($messageData, $isChannel = false) {
  844. $result = array();
  845. $result['message_id'] = $messageData['message_id'];
  846. if (!$isChannel) {
  847. //normal messages/groups
  848. $result['from']['id'] = $messageData['from']['id'];
  849. $result['from']['first_name'] = $messageData['from']['first_name'];
  850. @$result['from']['username'] = $messageData['from']['username'];
  851. @$result['from']['language_code'] = $messageData['from']['language_code'];
  852. } else {
  853. //channel posts
  854. $result['from']['id'] = $messageData['sender_chat']['id'];
  855. $result['from']['first_name'] = $messageData['sender_chat']['title'];
  856. @$result['from']['username'] = $messageData['sender_chat']['username'];
  857. @$result['from']['language_code'] = '';
  858. }
  859. $result['chat']['id'] = $messageData['chat']['id'];
  860. $result['date'] = $messageData['date'];
  861. $result['chat']['type'] = $messageData['chat']['type'];
  862. @$result['text'] = $messageData['text'];
  863. @$result['contact'] = $messageData['contact'];
  864. @$result['photo'] = $messageData['photo'];
  865. @$result['document'] = $messageData['document'];
  866. //photos and documents have only caption
  867. if (!empty($result['photo']) or ! empty($result['document'])) {
  868. @$result['text'] = $messageData['caption'];
  869. }
  870. @$result['voice'] = $messageData['voice'];
  871. @$result['audio'] = $messageData['audio'];
  872. @$result['video_note'] = $messageData['video_note'];
  873. @$result['location'] = $messageData['location'];
  874. @$result['sticker'] = $messageData['sticker'];
  875. @$result['new_chat_member'] = $messageData['new_chat_member'];
  876. @$result['new_chat_members'] = $messageData['new_chat_members'];
  877. @$result['new_chat_participant'] = $messageData['new_chat_participant'];
  878. @$result['left_chat_member'] = $messageData['left_chat_member'];
  879. @$result['left_chat_participant'] = $messageData['left_chat_participant'];
  880. @$result['reply_to_message'] = $messageData['reply_to_message'];
  881. //decode replied message too if received
  882. if ($result['reply_to_message']) {
  883. $result['reply_to_message'] = $this->preprocessMessageData($result['reply_to_message']);
  884. }
  885. //Uncomment following line for total debug
  886. //@$result['rawMessageData'] = $messageData;
  887. return ($result);
  888. }
  889. /**
  890. * Returns webhook data
  891. *
  892. * @param bool $rawData receive raw reply or preprocess to something more simple.
  893. *
  894. * @return array
  895. */
  896. public function getHookData($rawData = false) {
  897. $result = array();
  898. $postRaw = file_get_contents('php://input');
  899. if (!empty($postRaw)) {
  900. $postRaw = json_decode($postRaw, true);
  901. if ($this->debug) {
  902. debarr($result);
  903. }
  904. if (!$rawData) {
  905. if (isset($postRaw['message'])) {
  906. if (isset($postRaw['message']['from'])) {
  907. $result = $this->preprocessMessageData($postRaw['message']);
  908. }
  909. } else {
  910. if (isset($postRaw['channel_post'])) {
  911. $result = $this->preprocessMessageData($postRaw['channel_post'], true);
  912. } else {
  913. //other object like chat_member or message_reaction etc
  914. if (is_array($postRaw) and !empty($postRaw)) {
  915. $result = $postRaw;
  916. }
  917. }
  918. }
  919. } else {
  920. $result = $postRaw;
  921. }
  922. }
  923. return ($result);
  924. }
  925. /**
  926. * Sends an action to a chat using the Telegram API.
  927. *
  928. * @param string $chatid The ID of the chat.
  929. * @param string $action The action to be sent. Like "typing".
  930. *
  931. * @return string The result of the API request.
  932. * @throws Exception If the bot token is empty.
  933. */
  934. public function apiSendAction($chatid, $action) {
  935. $result = '';
  936. $method = 'sendChatAction';
  937. $data['chat_id'] = $chatid;
  938. $data['action'] = $action;
  939. if ($this->debug) {
  940. debarr($data);
  941. }
  942. $data_json = json_encode($data);
  943. if (!empty($this->botToken)) {
  944. $url = $this->apiUrl . $this->botToken . '/' . $method;
  945. if ($this->debug) {
  946. deb($url);
  947. }
  948. $ch = curl_init();
  949. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  950. curl_setopt($ch, CURLOPT_URL, $url);
  951. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  952. curl_setopt($ch, CURLOPT_POST, 1);
  953. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  954. if ($this->debug) {
  955. $result = curl_exec($ch);
  956. deb($result);
  957. $curlError = curl_error($ch);
  958. if (!empty($curlError)) {
  959. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  960. } else {
  961. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  962. }
  963. } else {
  964. $result = curl_exec($ch);
  965. }
  966. curl_close($ch);
  967. } else {
  968. throw new Exception('EX_TOKEN_EMPTY');
  969. }
  970. return ($result);
  971. }
  972. }