首页 > 解决方案 > 让电报机器人在将文档发送到 C# 中的群聊时做出反应

问题描述

我希望我的机器人在检测到文档在聊天中发送时发送消息。

该代码适用于直接与机器人聊天,但如果我将机器人添加到组中,如果我在组中发送文档,机器人将没有响应。

static void Main(string[] args)
{
      bot.OnMessage += Bot_OnMessage;
      bot.OnMessageEdited += Bot_OnMessage;
      bot.StartReceiving();
}

private static void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
{
      Console.WriteLine(e.Message.Type);
}

为什么我在群组中发送文件时机器人没有响应?谢谢!

标签: c#apitelegramtelegram-bot

解决方案


你必须要做的事情:

  • 通过BotFather检查您的机器人上的隐私模式将其关闭
  • 您的代码将仅在控制台上打印,它不会发送聊天,请执行以下代码以发送聊天:
static void Main(string[] args)
{
      bot.OnMessage += Bot_OnMessage;
      bot.OnMessageEdited += Bot_OnMessage;
      bot.StartReceiving();
}

private static async void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
{
    Console.WriteLine(e.Message.Type);
    if (e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Document)
    {
        // Send in chat
        await bot.SendTextMessageAsync(e.Message.Chat, "*This is a document!*", ParseMode.Markdown);
    }
}

什么是隐私模式

您在隐私模式下的机器人只能读取:

  1. 回复其消息
  2. /所有命令(任何以/help、/settings 等开头的单词)
  3. 机器人提及(如@MyBot 等)
  4. 服务消息(如添加聊天成员、更改聊天标题等)

这将确保您的机器人用户不会听到您的聊天记录。

具有隐私模式的机器人它将显示:无法访问消息

把它关掉


推荐阅读