php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 955 forks source link

Sending Multiple messages #1277

Closed pushpenderpannu closed 2 years ago

pushpenderpannu commented 2 years ago

🎉 Feature Request

Summary

Right now we have $this->replyToChat("test");

There should be another method that accepts an array of strings and send them as individual messages.

jacklul commented 2 years ago

You can also:

$chat_id = 123456;
$messages = [
    'test 1',
    'test 2',
    'test 3'
];

foreach ($messages as $message) {
    $response = Request::sendMessage([
        'chat_id' => $chat_id,
        'text' => $message
    ]);
}

return $response;
pushpenderpannu commented 2 years ago

In this, I would have to check when is the last message, so that I can capture ServerResponse and return that in Command execute.

jacklul commented 2 years ago

You could make a trait and import it in your commands when needed:

(untested code)

<?php

namespace Longman\TelegramBot\Commands;

trait ReplyToChatMultiple
{
    public function ReplyToChatMultiple(array $messages, array $data = []): ServerResponse
    {
        if ($message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost()) {
            foreach ($messages as $index => $text) {
                if (empty($text)) {
                    throw new \Exception('Missing text at array index ' . $index);
                }

                $status = Request::sendMessage(array_merge([
                    'chat_id' => $message->getChat()->getId(),
                    'text' => $text,
                ], $data));
                // It might be good idea to have $status->isOk() check here and log failures
            }

            return $status;
        }

        return Request::emptyResponse();
    }
}

Then import it in your command:

<?php

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Commands\ReplyToChatMultiple;

class TestCommand extends UserCommand
{
    use ReplyToChatMultiple;

...

It's possible you could make this available in all your commands by creating overrides for src/Commands/XXXCommand.php classes but I'm not 100% sure:

<?php

namespace MyProject/TelegramBot;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Commands\ReplyToChatMultiple;

class NewUserCommand extends UserCommand
{
    use ReplyToChatMultiple;
}

and then in your user commands:

<?php

use MyProject\TelegramBot\NewUserCommand;

class TestCommand extends NewUserCommand
{
...

Similar procedure for SystemCommand and AdminCommand classes.

jacklul commented 2 years ago

I am assuming we can close down this issue?

pushpenderpannu commented 2 years ago

Yes. Thank you for your help.

On Wed, Jan 19, 2022 at 5:33 PM jacklul @.***> wrote:

I am assuming we can close down this issue?

— Reply to this email directly, view it on GitHub https://github.com/php-telegram-bot/core/issues/1277#issuecomment-1016401619, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARA4LRA74R6KQKX7RBELTE3UW2SC3ANCNFSM5MGG3BXA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

-- Thank you, Pushpender Pannu