SendtochannelCommand.php 1.83 KB
Newer Older
MBoretto's avatar
MBoretto committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

/*
 * This file is part of the TelegramBot package.
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;

use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;

class SendtochannelCommand extends Command
{
    protected $name = 'sendtochannel';
MBoretto's avatar
MBoretto committed
22
    protected $description = 'Send message to a channel';
MBoretto's avatar
MBoretto committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    protected $usage = '/sendchannel <message to send>';
    protected $version = '0.1.0';
    protected $enabled = true;
    protected $public = true;
    //need Mysql
    protected $need_mysql = false;

    public function execute()
    {
        $update = $this->getUpdate();
        $message = $this->getMessage();

        $chat_id = $message->getChat()->getId();
        $message_id = $message->getMessageId();
        $text = $message->getText(true);
        if (empty($text)) {
            $text_back = 'Write the message to sent: /sendtochannel <message>';
        } else {
MBoretto's avatar
MBoretto committed
41
            $your_channel = $this->getConfig('your_channel');
MBoretto's avatar
MBoretto committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            //Send message to channel
            $data = [];
            $data['chat_id'] = $your_channel;
            $data['text'] = $text;

            $result = Request::sendMessage($data);
            if ($result->isOk()) {
                $text_back = 'Message sent succesfully to: '.$your_channel;
            } else {
                $text_back = 'Sorry message not sent to: '.$your_channel;
            }
        }

        $data = [];
        $data['chat_id'] = $chat_id;
        $data['text'] = $text_back;

        $result = Request::sendMessage($data);
        return $result;
    }
}