-
-
Notifications
You must be signed in to change notification settings - Fork 963
How to send message to channel user without user interaction
Arief Bayu Purwanto edited this page Feb 13, 2016
·
3 revisions
Based on discussion in this thread: https://github.com/akalongman/php-telegram-bot/issues/71#issuecomment-183177739
To send message to user/channel without initiating message from user (usually, for broadcasting message), you can use the following snippet:
<?php
$loader = require __DIR__.'/vendor/autoload.php';
use Longman\TelegramBot\Request;
$API_KEY = '--botfather-api-keu--';
$BOT_NAME = '--bofather-bot-name--';
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
$message = isset($argv[2]) ? $argv[2] : '';
$telegramId = isset($argv[1]) ? $argv[1] : '';
if($message !== '' && $telegramId !== ''){
$data = [];
$data['chat_id'] = $telegramId;
$data['text'] = $message;
$result = Request::sendMessage($data);
if ($result->isOk()) {
echo 'Message sent succesfully to: '.$data['chat_id'] ;
} else {
echo 'Sorry message not sent to: '.$data['chat_id'] ;
}
}
//to use it, just call: $php broadcast.php [telegram-chat-id] [message]