api.telegram.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. rcms_delete_files(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. //POST data encoding
  579. $data_json = json_encode($data);
  580. if (!empty($this->botToken)) {
  581. $url = $this->apiUrl . $this->botToken . '/' . $method;
  582. if ($this->debug) {
  583. deb($url);
  584. }
  585. $ch = curl_init();
  586. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  587. curl_setopt($ch, CURLOPT_URL, $url);
  588. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  589. curl_setopt($ch, CURLOPT_POST, 1);
  590. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  591. if ($this->debug) {
  592. $result = curl_exec($ch);
  593. deb($result);
  594. $curlError = curl_error($ch);
  595. if (!empty($curlError)) {
  596. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  597. } else {
  598. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  599. }
  600. } else {
  601. $result = curl_exec($ch);
  602. }
  603. curl_close($ch);
  604. } else {
  605. throw new Exception('EX_TOKEN_EMPTY');
  606. }
  607. return ($result);
  608. }
  609. /**
  610. * Sets HTTPS web hook URL for some bot
  611. *
  612. * @param string $webHookUrl HTTPS url to send updates to. Use an empty string to remove webhook integration
  613. * @param int $maxConnections Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
  614. *
  615. * @return string
  616. */
  617. public function setWebHook($webHookUrl, $maxConnections = 40) {
  618. $result = '';
  619. if (!empty($this->botToken)) {
  620. $data = array();
  621. if (!empty($webHookUrl)) {
  622. $method = 'setWebhook';
  623. if (ispos($webHookUrl, 'https://')) {
  624. $data['url'] = $webHookUrl;
  625. $data['max_connections'] = $maxConnections;
  626. } else {
  627. throw new Exception('EX_NOT_SSL_URL');
  628. }
  629. } else {
  630. $method = 'deleteWebhook';
  631. }
  632. $url = $this->apiUrl . $this->botToken . '/' . $method;
  633. if ($this->debug) {
  634. deb($url);
  635. }
  636. $ch = curl_init();
  637. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  638. curl_setopt($ch, CURLOPT_URL, $url);
  639. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  640. curl_setopt($ch, CURLOPT_POST, 1);
  641. if (!empty($data)) {
  642. $data_json = json_encode($data);
  643. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  644. }
  645. if ($this->debug) {
  646. $result = curl_exec($ch);
  647. deb($result);
  648. $curlError = curl_error($ch);
  649. if (!empty($curlError)) {
  650. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  651. } else {
  652. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  653. }
  654. } else {
  655. $result = curl_exec($ch);
  656. }
  657. curl_close($ch);
  658. } else {
  659. throw new Exception('EX_TOKEN_EMPTY');
  660. }
  661. return ($result);
  662. }
  663. /**
  664. * Returns bot web hook info
  665. *
  666. * @return string
  667. */
  668. public function getWebHookInfo() {
  669. $result = '';
  670. if (!empty($this->botToken)) {
  671. $method = 'getWebhookInfo';
  672. $url = $this->apiUrl . $this->botToken . '/' . $method;
  673. if ($this->debug) {
  674. deb($url);
  675. }
  676. $ch = curl_init();
  677. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  678. curl_setopt($ch, CURLOPT_URL, $url);
  679. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  680. if ($this->debug) {
  681. $result = curl_exec($ch);
  682. deb($result);
  683. $curlError = curl_error($ch);
  684. if (!empty($curlError)) {
  685. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  686. } else {
  687. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  688. }
  689. } else {
  690. $result = curl_exec($ch);
  691. }
  692. curl_close($ch);
  693. }
  694. return ($result);
  695. }
  696. /**
  697. * Returns chat data array by its chatId
  698. *
  699. * @param int chatId
  700. *
  701. * @return array
  702. */
  703. public function getChatInfo($chatId) {
  704. $result = array();
  705. if (!empty($this->botToken) and (!empty($chatId))) {
  706. $method = 'getChat';
  707. $url = $this->apiUrl . $this->botToken . '/' . $method . '?chat_id=' . $chatId;
  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. if (!empty($result)) {
  729. $result = json_decode($result, true);
  730. }
  731. }
  732. return ($result);
  733. }
  734. /**
  735. * Returns file path by its file ID
  736. *
  737. * @param string $fileId
  738. *
  739. * @return string
  740. */
  741. public function getFilePath($fileId) {
  742. $result = '';
  743. if (!empty($this->botToken)) {
  744. $method = 'getFile';
  745. $url = $this->apiUrl . $this->botToken . '/' . $method . '?file_id=' . $fileId;
  746. if ($this->debug) {
  747. deb($url);
  748. }
  749. $ch = curl_init();
  750. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  751. curl_setopt($ch, CURLOPT_URL, $url);
  752. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  753. if ($this->debug) {
  754. $result = curl_exec($ch);
  755. deb($result);
  756. $curlError = curl_error($ch);
  757. if (!empty($curlError)) {
  758. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  759. } else {
  760. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  761. }
  762. } else {
  763. $result = curl_exec($ch);
  764. }
  765. curl_close($ch);
  766. if (!empty($result)) {
  767. $result = json_decode($result, true);
  768. if (@$result['ok']) {
  769. //we got it!
  770. $result = $result['result']['file_path'];
  771. } else {
  772. //something went wrong
  773. $result = '';
  774. }
  775. }
  776. }
  777. return ($result);
  778. }
  779. /**
  780. * Returns some file content
  781. *
  782. * @param string $filePath
  783. *
  784. * @return mixed
  785. */
  786. public function downloadFile($filePath) {
  787. $result = '';
  788. if (!empty($this->botToken)) {
  789. $cleanApiUrl = str_replace('bot', '', $this->apiUrl);
  790. $url = $cleanApiUrl . 'file/bot' . $this->botToken . '/' . $filePath;
  791. $ch = curl_init();
  792. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  793. curl_setopt($ch, CURLOPT_URL, $url);
  794. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  795. $result = curl_exec($ch);
  796. curl_close($ch);
  797. }
  798. return ($result);
  799. }
  800. /**
  801. * Returns preprocessed message in standard, fixed fields format
  802. *
  803. * @param array $messageData
  804. * @param bool $isChannel
  805. *
  806. * @return array
  807. */
  808. protected function preprocessMessageData($messageData, $isChannel = false) {
  809. $result = array();
  810. $result['message_id'] = $messageData['message_id'];
  811. if (!$isChannel) {
  812. //normal messages/groups
  813. $result['from']['id'] = $messageData['from']['id'];
  814. $result['from']['first_name'] = $messageData['from']['first_name'];
  815. @$result['from']['username'] = $messageData['from']['username'];
  816. @$result['from']['language_code'] = $messageData['from']['language_code'];
  817. } else {
  818. //channel posts
  819. $result['from']['id'] = $messageData['sender_chat']['id'];
  820. $result['from']['first_name'] = $messageData['sender_chat']['title'];
  821. @$result['from']['username'] = $messageData['sender_chat']['username'];
  822. @$result['from']['language_code'] = '';
  823. }
  824. $result['chat']['id'] = $messageData['chat']['id'];
  825. $result['date'] = $messageData['date'];
  826. $result['chat']['type'] = $messageData['chat']['type'];
  827. @$result['text'] = $messageData['text'];
  828. @$result['photo'] = $messageData['photo'];
  829. @$result['document'] = $messageData['document'];
  830. //photos and documents have only caption
  831. if (!empty($result['photo']) or ! empty($result['document'])) {
  832. @$result['text'] = $messageData['caption'];
  833. }
  834. @$result['voice'] = $messageData['voice'];
  835. @$result['audio'] = $messageData['audio'];
  836. @$result['video_note'] = $messageData['video_note'];
  837. @$result['location'] = $messageData['location'];
  838. @$result['sticker'] = $messageData['sticker'];
  839. @$result['new_chat_member'] = $messageData['new_chat_member'];
  840. @$result['left_chat_member'] = $messageData['left_chat_member'];
  841. @$result['reply_to_message'] = $messageData['reply_to_message'];
  842. //decode replied message too if received
  843. if ($result['reply_to_message']) {
  844. $result['reply_to_message'] = $this->preprocessMessageData($result['reply_to_message']);
  845. }
  846. return ($result);
  847. }
  848. /**
  849. * Returns webhook data
  850. *
  851. * @param bool $rawData receive raw reply or preprocess to something more simple.
  852. *
  853. * @return array
  854. */
  855. public function getHookData($rawData = false) {
  856. $result = array();
  857. $postRaw = file_get_contents('php://input');
  858. if (!empty($postRaw)) {
  859. $postRaw = json_decode($postRaw, true);
  860. if ($this->debug) {
  861. debarr($result);
  862. }
  863. if (!$rawData) {
  864. if (isset($postRaw['message'])) {
  865. if (isset($postRaw['message']['from'])) {
  866. $result = $this->preprocessMessageData($postRaw['message']);
  867. }
  868. } else {
  869. if (isset($postRaw['channel_post'])) {
  870. $result = $this->preprocessMessageData($postRaw['channel_post'], true);
  871. }
  872. }
  873. } else {
  874. $result = $postRaw;
  875. }
  876. }
  877. return ($result);
  878. }
  879. /**
  880. * Sends an action to a chat using the Telegram API.
  881. *
  882. * @param string $chatid The ID of the chat.
  883. * @param string $action The action to be sent. Like "typing".
  884. *
  885. * @return string The result of the API request.
  886. * @throws Exception If the bot token is empty.
  887. */
  888. public function apiSendAction($chatid, $action) {
  889. $result = '';
  890. $method = 'sendChatAction';
  891. $data['chat_id'] = $chatid;
  892. $data['action'] = $action;
  893. if ($this->debug) {
  894. debarr($data);
  895. }
  896. $data_json = json_encode($data);
  897. if (!empty($this->botToken)) {
  898. $url = $this->apiUrl . $this->botToken . '/' . $method;
  899. if ($this->debug) {
  900. deb($url);
  901. }
  902. $ch = curl_init();
  903. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  904. curl_setopt($ch, CURLOPT_URL, $url);
  905. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  906. curl_setopt($ch, CURLOPT_POST, 1);
  907. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
  908. if ($this->debug) {
  909. $result = curl_exec($ch);
  910. deb($result);
  911. $curlError = curl_error($ch);
  912. if (!empty($curlError)) {
  913. show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError);
  914. } else {
  915. show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success'));
  916. }
  917. } else {
  918. $result = curl_exec($ch);
  919. }
  920. curl_close($ch);
  921. } else {
  922. throw new Exception('EX_TOKEN_EMPTY');
  923. }
  924. return ($result);
  925. }
  926. }