1
votes

I used php-telegram-bot/core to create a shopping bot in telegram.

What I want to do is when a User make an order, bot send a notification that a new Order is come to admin of Channel.

Suppose admin channel username is like @admin_username and stored in a global variable(means that may be change in a period of time). for that I wrote this :

    static public function showOrderToConfirm ($order)
    {

        if ($order) {
            Request::sendMessage([
                'chat_id'    => '@admin_username',
                'text'       => 'New Order registered',
                'parse_mode' => 'HTML'
            ]);
        }

    }

But this does not work and does not anything.

2

2 Answers

2
votes

Telegram bot API does not support sending messages using username because it's not a stable item and can be changed by the user. On the other hand, bots can only send messages to user that have sent at least one message to the bot before.

You know that when a user sends a message to a bot (for example the users taps on the start button) the bot can get his/her username and ChatID (As you know ChatID is different from username; ChatID is a long number) so I think the best way that you can fix this issue is storing the chat IDs and related usernames in a database and send the message to that chatID of your favorite username.

By the way, try searching online to see whether there is an API which supports sending messages to usernames or not. But as I know it's not possible.

0
votes

This example works very well:

<?php
$token = 'YOUR_TOCKEN_HERE';
$website = 'https://api.telegram.org/bot' . $token;

$input = file_get_contents('php://input');
$update = json_decode($input, true);

$chatId = $update['message']['chat']['id'];
$message = $update['message']['text'];

switch ($message) {
case '/start':
    $response = 'now bot is started';
    sendMessage($chatId, $response);
    break;
case '/info':
    $response = 'Hi, i am  @trecno_bot';
    sendMessage($chatId, $response);
    break;
default:
    $response = 'Sorry, i can not understand you';
    sendMessage($chatId, $response);
    break;
}

function sendMessage($chatId, $response){
    $url = $GLOBALS['website'] . '/sendMessage?chat_id=' . $chatId .         
    '&parse_mode=HTML&text=' . urlencode($response);
    file_get_contents($url);
 }


?>