Skip to content

Instantly share code, notes, and snippets.

@noplanman
Forked from ariefbayu/broadcast.php
Last active July 3, 2020 07:00
Show Gist options
  • Save noplanman/5f81bf74a8defccb4509 to your computer and use it in GitHub Desktop.
Save noplanman/5f81bf74a8defccb4509 to your computer and use it in GitHub Desktop.
<?php
/**
* Usage on CLI: $ php broadcast.php [telegram-chat-id] [message]
*/
require __DIR__ . '/vendor/autoload.php';
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
$API_KEY = '--botfather-api-key--';
$BOT_NAME = '--botfather-bot-name--';
$telegram = new Telegram($API_KEY, $BOT_NAME);
// Get the chat id and message text from the CLI parameters.
$chat_id = isset($argv[1]) ? $argv[1] : '';
$message = isset($argv[2]) ? $argv[2] : '';
if ($chat_id !== '' && $message !== '') {
$data = [
'chat_id' => $chat_id,
'text' => $message,
];
$result = Request::sendMessage($data);
if ($result->isOk()) {
echo 'Message sent succesfully to: ' . $chat_id . "\n";
} else {
echo 'Sorry message not sent to: ' . $chat_id . "\n";
}
}
@noplanman
Copy link
Author

Outgoing messages aren't saved to the database, you will have to do that yourself. You can create your own table in the database and fill it with the message content.

Hope that helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment