c# - 让电报机器人在将文档发送到 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);
}
为什么我在群组中发送文件时机器人没有响应?谢谢!
解决方案
你必须要做的事情:
- 通过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);
}
}
什么是隐私模式
您在隐私模式下的机器人只能读取:
- 回复其消息
/
所有命令(任何以/help、/settings 等开头的单词)- 机器人提及(如@MyBot 等)
- 服务消息(如添加聊天成员、更改聊天标题等)
这将确保您的机器人用户不会听到您的聊天记录。
具有隐私模式的机器人它将显示:无法访问消息
把它关掉
推荐阅读
- pine-script - 如何制作和指标仅在 4 小时时间范围内显示?
- mysql - 继续添加外键
- azure - azure ml - 如何从存储帐户中的文件共享注册训练数据集(表格)
- html - 如何垂直对齐图像和文本?
- json - 未处理的异常:FormatException:在颤振中使用 json.decode 时出现意外字符
- python - 条形图的注释
- c# - 如何确保 Addin / xll 与 C# 以编程方式创建的 Excel 工作簿“打包”?
- laravel - 使用summernote时没有保存说明
- swiftui - 我可以在 swiftUI 中将一个数组放入另一个数组吗?
- grammar - 形式语法去除单元生产