Discussion of article "How to create bots for Telegram in MQL5" - page 13

 

Good afternoon.

Can the bot receive messages through the channel?

I have assigned the bot as an administrator. New messages it sees, but the text is always a blank line.

msg.message_text=item["message"]["text"].ToStr();
msg.message_text=StringDecode(msg.message_text);
Here it is always a blank line.
 
leonerd:

Good afternoon.

Can the bot receive messages through the channel?

I have assigned the bot as an administrator. It sees new messages, but the text is always a blank line.

Here it is always a blank line.

In the channel_post field everything comes in https://core.telegram.org/bots/api#update.

It would be nice to refine the class. I will add the channel_post_text field to CCustomMessage. And fill it in GetUpdates().

Telegram Bot API
  • core.telegram.org
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. To learn how to create and set up a bot, please consult our Introduction to Bots and Bot FAQ. Recent changes April 14, 2019 Added support for native polls: added the object Poll, the methods sendPoll and stopPoll and the field poll in the Message...
 
            msg.chat_id=item["message"]["chat"]["id"].ToInt();

            msg.chat_first_name=item["message"]["chat"]["first_name"].ToStr();
            msg.chat_first_name=StringDecode(msg.chat_first_name);

            msg.chat_last_name=item["message"]["chat"]["last_name"].ToStr();
            msg.chat_last_name=StringDecode(msg.chat_last_name);

            msg.chat_username=item["message"]["chat"]["username"].ToStr();
            msg.chat_username=StringDecode(msg.chat_username);

            msg.chat_type=item["message"]["chat"]["type"].ToStr();
Only for some reason for chat from the channel all these fields are null.
 
leonerd:
Only for some reason all these fields are null for chat from a channel.

For a channel you should look at the channel_post field, not the message field. channel_post is also of Message type.

I get the text:

msg.channel_post_text=item["channel_post"]["text"].ToStr();
msg.channel_post_text=StringDecode(msg.channel_post_text);

But other fields are not:

msg.channel_post_chat_id=item["channel_post"]["chat"]["id"].ToInt();
//---
msg.channel_post_from_first_name=item["channel_post"]["from"]["first_name"].ToStr();     
msg.channel_post_from_first_name=StringDecode(msg.channel_post_from_first_name);
 
//---
            msg.channel_post_text=item["channel_post"]["text"].ToStr();
            msg.channel_post_text=StringDecode(msg.channel_post_text);

            long msg_id=item["channel_post"]["message_id"].ToInt();
            long msg_date=item["channel_post"]["date"].ToInt();



            //---
            msg.channel_post_chat_id=item["channel_post"]["chat"]["id"].ToInt();
            //---
            msg.channel_post_from_first_name=item["channel_post"]["from"]["first_name"].ToStr();     
            msg.channel_post_from_first_name=StringDecode(msg.channel_post_from_first_name);

The first three fields receive the value. But the last three fields (for fields like Chat and From) do not receive the value. Where to look?
 

Figured it out.

channel_post.chat_id - can be negative, and I took it for a crooked value.

channel_post.from.first_name - from - optional field, can be without it. In general, everything is here https://core.telegram.org/bots/api#message

Telegram Bot API
  • core.telegram.org
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. To learn how to create and set up a bot, please consult our Introduction to Bots and Bot FAQ. Recent changes April 14, 2019 Added support for native polls: added the object Poll, the methods sendPoll and stopPoll and the field poll in the Message...
 

I tried this code in OnInit() to send a message to my channel. but it does not work, can you give me example on how to send message to a channel or group chat?

thanks

bot.SendMessage("t.me/BotSignals","Hello",false,false);

 

Please help me with advice. I added theterminal settings. Through the browser the request is executed successfully https://api.telegram.org/bot&lt;token>/sendMessage?chat_id=<id>&text=test.

The lines of code below are executed, but SendMessage returns error code 5203 (Error as a result of HTTP request execution). The bot.GetMe() method returns the same 5203 error.

What could be the problem? Thanks!

      bot.Token("<token>"); //set token
  int _res = bot.SendMessage(<id>, "test"); //your ID
Bots: An introduction for developers
Bots: An introduction for developers
  • core.telegram.org
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our bot API. 1. What can I do with bots? To name just a few things, you could use bots to: Get customized notifications and news. A bot can act as a smart...
 
Thank you all! I figured it out. I guess the problem is RF and blocking.
 
Hello, I have taken a bot that will share signals through a group. I ran the code through MetaEditor, changed it and can't compile it. I got so many errors
Files:
Screenshot_3.jpg  227 kb