【问题标题】:How to get updates and reply telegram bot?如何获取更新和回复电报机器人?
【发布时间】:2017-11-04 06:16:12
【问题描述】:

这是我尝试连接到电报机器人的代码

namespace telegramUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TelegramBotClient bot = new TelegramBotClient("xxxxxxxxx");
        int offset = 23;
        Update temp = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                try
                {
                    var m = Task.Run(async()=> bot.GetUpdatesAsync(offset,50)).Result;
                    foreach (var x in m.Result)
                    {
                        switch (x.Type)
                        {
                            case UpdateType.MessageUpdate:
                                temp = x;
                                backgroundWorker1.ReportProgress(0);
                                bot.SendTextMessageAsync(x.Message.Chat.Id, ":)").ConfigureAwait(false);
                                break;
                        }
                        offset = x.Id+1;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text = temp.Message.From.FirstName;
        }
    }
}

在破坏消息框后显示“发生一个或多个错误”。问题是什么?电报机器人应该回复“:)”但它没有。我不知道我是否收到任何更新。

【问题讨论】:

  • 我有一个机器人,我想在机器人上创建聊天模块,这样可以吗?你能帮我吗
  • 请更多关于您的问题的前飞机...

标签: c# bots telegram telegram-bot


【解决方案1】:

试试这个:

 bot.SendTextMessageAsync(x.Message.Chat.Id, ":)").GetAwaiter().GetResult();

你没有执行任务。就这样

【讨论】:

【解决方案2】:

对我来说,最近在制作 bot.SendTextMessageAsync 时发生了同样的错误(代码在 2020 年 2 月工作,但后来没有工作)我收到了同样的异常。经过一番研究,我发现了这个话题 [Telegram Bot stops with An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll 这实际上解决了我的问题。

    using System.Net;

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2017-08-18
    • 2021-10-25
    相关资源
    最近更新 更多