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
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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";
}
}
@rtorrescodes
Copy link

Hello, this code is great, you help me a lot. Can you help me telling me what to do so this messages gets into the database? im using the getUpdates method and i want these messages to be reported there... thanks

@rtorrescodes
Copy link

I understand that the messages that people enter in the chat goes to the mysql database, yes, i got them and i use them. Is there any way i can add something to this broadcast.php code so the messages from the bot get saved in the mysql database too? Thanks a lot.

@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