Send message to all active chats
public static sendToActiveChats ( string $callback_function, array $data, boolean $send_groups = true, boolean $send_super_groups = true, boolean $send_users = true, string $date_from = null, string $date_to = null ) : array | ||
$callback_function | string | |
$data | array | |
$send_groups | boolean | |
$send_super_groups | boolean | |
$send_users | boolean | |
$date_from | string | |
$date_to | string | |
리턴 | array |
public function execute() { $update = $this->getUpdate(); $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $message_id = $message->getMessageId(); $text = $message->getText(true); if (empty($text)) { $text = 'Write te message to sent: /sendall <message>'; } else { $results = Request::sendToActiveChats('sendMessage', array('text' => $text), true, true, null, null); $tot = 0; $fail = 0; $text = "Message sended to:\n"; foreach ($results as $result) { $status = ''; $type = ''; print_r($result); if ($result->isOk()) { $status = '✔️'; $ServerResponse = $result->getResult(); $chat = $ServerResponse->getChat(); if ($chat->isPrivateChat()) { $name = $chat->getFirstName(); $type = 'user'; } else { $name = $chat->getTitle(); $type = 'chat'; } } else { $status = '✖️'; ++$fail; } ++$tot; $text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n"; } $text .= "Delivered: " . ($tot - $fail) . '/' . $tot . "\n"; } if ($tot == 0) { $text = "No users or chats found.."; } $data = array(); $data['chat_id'] = $chat_id; //$data['reply_to_message_id'] = $message_id; $data['text'] = $text; $result = Request::sendMessage($data); return $result; }
/** * Execute command * * @return boolean * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() { $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $text = $message->getText(true); if ($text === '') { $text = 'Write the message to send: /sendtoall <message>'; } else { $results = Request::sendToActiveChats('sendMessage', ['text' => $text], true, true, true, null, null); $total = 0; $failed = 0; $text = 'Message sent to:' . "\n"; /** @var ServerResponse $result */ foreach ($results as $result) { $name = ''; $type = ''; if ($result->isOk()) { $status = '✔️'; /** @var Message $message */ $message = $result->getResult(); $chat = $message->getChat(); if ($chat->isPrivateChat()) { $name = $chat->getFirstName(); $type = 'user'; } else { $name = $chat->getTitle(); $type = 'chat'; } } else { $status = '✖️'; ++$failed; } ++$total; $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . "\n"; } $text .= 'Delivered: ' . ($total - $failed) . '/' . $total . "\n"; if ($total === 0) { $text = 'No users or chats found..'; } } $data = ['chat_id' => $chat_id, 'text' => $text]; return Request::sendMessage($data); }
foreach ($triggers as $trigger) { $errors[] = $trigger->description; } } catch (Exception $e) { // Exception in ZabbixApi catched. echo $e->getMessage(); } try { // create Telegram API object $telegram = new Telegram($API_KEY, $BOT_NAME); $telegram->enableMySQL($credentials); $telegram->addCommandsPath($COMMANDS_FOLDER); $telegram->setLogRequests(true); $telegram->setLogPath('logs/' . $BOT_NAME . '.log'); $telegram->setLogVerbosity(3); if (!empty($errors)) { $results = Request::sendToActiveChats('sendMessage', array('text' => "[Zabbix]\nWe have a problem\n" . implode(', ', $errors)), false, true, null, null); } $ServerResponse = $telegram->handleGetUpdates(); if ($ServerResponse->isOk()) { $n_update = count($ServerResponse->getResult()); print date('Y-m-d H:i:s', time()) . ' - Processed ' . $n_update . " updates\n"; } else { print date('Y-m-d H:i:s', time()) . " - Fail fetch updates\n"; print $ServerResponse->printError() . "\n"; } } catch (TelegramException $e) { // log telegram errors print $e->getMessage(); $log->addError($e->getMessage()); }