Longman\TelegramBot\Request::sendMessage PHP Method

sendMessage() public static method

Use this method to send text messages. On success, the sent Message is returned
public static sendMessage ( array $data ) : ServerResponse
$data array
return Longman\TelegramBot\Entities\ServerResponse
    public static function sendMessage(array $data)
    {
        $text = $data['text'];
        do {
            //Chop off and send the first message
            $data['text'] = mb_substr($text, 0, 4096);
            $response = self::send('sendMessage', $data);
            //Prepare the next message
            $text = mb_substr($text, 4096);
        } while (mb_strlen($text, 'UTF-8') > 0);
        return $response;
    }

Usage Example

 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 {
         $your_channel = $this->getConfig('your_channel');
         //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;
 }
All Usage Examples Of Longman\TelegramBot\Request::sendMessage