接收电报聊天ID C#

发布于 2025-02-05 17:55:01 字数 183 浏览 1 评论 0原文

如何使用C#自动接收Telegram Chat.id?

需要从我的机器人到我的sendphoto。

Bot.SendPhotoAsync(chat.id, new InputOnlineFile(imageFile, "Spotted.Png"), "AnStuffSpotted.");

How to automatically receive telegram chat.id with c# ?

Need that for SendPhoto from my bot to me in tg.

Bot.SendPhotoAsync(chat.id, new InputOnlineFile(imageFile, "Spotted.Png"), "AnStuffSpotted.");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2025-02-12 17:55:01

当您拥有机器人的令牌时,您可以创建它并添加此事件处理程序:

var bot = new TelegramBot(token);
bot.NewTextMessage += this.OnBotMessage;
bot.ChatMembersAddedMessage += this.OnBotMessage;
bot.StartReceiving();

当聊天有消息或添加新成员时,将调用此方法:

private void OnBotMessage(object sender, TelegramMessageEventArgs e)
{
    var token = this.tokenTextBox.Text.Trim();
    var chatId = e.Message.Chat.Id.ToString();
    var chatName = e.Message.Chat.Title;

    //SaveBotSettings(token, chatId, chatName);
}

您可以使用此方法来保存令牌和chatid。您可以保存到数据库中,或者根据您的需求,将代码复制和使用(在某些const中进行了硬编码)。 chatid永远不会改变,因此独特的问题是获得该ID。一旦获得,您就可以永远使用。

When you have the Bot's token, you can create it and add this event handlers:

var bot = new TelegramBot(token);
bot.NewTextMessage += this.OnBotMessage;
bot.ChatMembersAddedMessage += this.OnBotMessage;
bot.StartReceiving();

When the chat has messages or a new member is added, this method will be invoked:

private void OnBotMessage(object sender, TelegramMessageEventArgs e)
{
    var token = this.tokenTextBox.Text.Trim();
    var chatId = e.Message.Chat.Id.ToString();
    var chatName = e.Message.Chat.Title;

    //SaveBotSettings(token, chatId, chatName);
}

You can use this method to save your token and chatId. You can save into a database or simply copy and use in your code (hardcoded in some const) depending of your needs. The chatId never change so the unique problem is get that id. Once you get, you can use forever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文