文章 "如何采用 MQL5 创建用于 Telegram 的 bots" - 页 13

 

下午好。

机器人可以通过通道接收信息吗?

我已将机器人指定为管理员。它能看到新信息,但文本总是空一行。

msg.message_text=item["message"]["text"].ToStr();
msg.message_text=StringDecode(msg.message_text);
这里总是空行。
 
leonerd:

下午好。

机器人可以通过通道接收信息吗?

我已将机器人指定为管理员。它能看到新信息,但文本总是空行。

这里总是空一行。

在 channel_post 字段中,所有内容都是https://core.telegram.org/bots/api#update。

如果能完善这个类就更好了。我将在 CCustomMessage 中添加 channel_post_text 字段。并将其填入 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();
只是由于某种原因,频道聊天时所有这些字段都是空的。
 
leonerd:
只是由于某些原因,所有这些字段对于来自频道的聊天都是空的。

对于一个频道,您应该查看 channel_post 字段,而不是消息字段。channel_post 也属于消息类型。

我得到了文本:

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

但其他字段不是:

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);

前三个字段会收到值。但最后三个字段(聊天和发件人等字段)不接收值。去哪里找?
 

想出来了。

channel_post.chat_id - 可以是负值,我把它当成了一个错误的值。

channel_post.from.first_name - from - 可选字段,可以不填。总的来说,一切都在这里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...
 

我试着在 OnInit() 中用这段代码向我的频道发送信息。但它不起作用,你能举例说明如何向频道或群聊发送信息吗?

谢谢

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

 

请给我一些建议。我在终端设置 中添加了"https://api.telegram.org " 权限。通过浏览器成功执行了请求https://api.telegram.org/bot&lt;token>/sendMessage?chat_id=<id>&text=test。

下面的代码行已执行,但 SendMessage 返回错误代码 5203(HTTP 请求执行时出错)。bot.GetMe() 方法也返回同样的 5203 错误。

问题出在哪里?谢谢!

      bot.Token("<token>"); //设置令牌
  int _res = bot.SendMessage(<id>, "test"); //您的 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...
 
谢谢大家!我想明白了。我猜问题出在射频和阻塞上。
 
你好,我开发了一个机器人,可以通过群组共享信号。我在 MetaEditor 中运行了代码,进行了修改,但无法编译。我遇到了很多错误
附加的文件:
Screenshot_3.jpg  227 kb