A
A
Andrey Boychenko2019-04-22 22:09:03
PHP
Andrey Boychenko, 2019-04-22 22:09:03

Request handler from N telegram bots?

Good afternoon! Let's say I have N records in the database with keys to telegram bots. Of course, webhooks are delivered to different URLs, but they have one handler.
I have a method in which I create a connection, and then I go through them in a loop, but from whichever bot I send a message, only the first one responds. Tell me what exactly am I doing wrong?

public function hendle()
    {

        $bots = [];

        $bots[] = new Api('здесь первый ключ');
        $bots[] = new Api('здесь второй ключ');

        $instance = [];

        foreach ($bots as $key => $bot)
        {
            if ($bot->getWebhookUpdates())
            {
                $request = $bot->getWebhookUpdates();

                if(isset($request['message']))
                {
                    $instance['message'] = $request['message'];
                    $instance['bot'] = $bot;
                    $instance['me'] = $request->getMe();
                }
            }
        }

        $chat_id = $instance['message']['chat']['id'];

        return $instance['bot']->sendMessage([
            'chat_id' => $chat_id,
            'text' => "Hello World",
        ]);
    }

Where am I making a mistake and how to listen to any number of bots? Moreover, if I have many users, then for each of them there will be a request to the database where the api keys are stored. What will be more profitable in terms of load and speed? Store data with keys in config file or DB?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-04-22
@thewind

In your question and example - collapse. You write that webhooks are installed. But in this case, you do not need to request any updates - they themselves arrive at the url specified in the webhook. And in the same place you get the id of the bot and all the necessary data on the sender / recipient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question