Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取更新和回复电报机器人?_C#_Bots_Telegram_Telegram Bot - Fatal编程技术网

C# 如何获取更新和回复电报机器人?

C# 如何获取更新和回复电报机器人?,c#,bots,telegram,telegram-bot,C#,Bots,Telegram,Telegram Bot,这是我试图连接到电报机器人的代码 namespace telegramUpdate { public partial class Form1 : Form { public Form1() { InitializeComponent(); } TelegramBotClient bot = new TelegramBotClient("xxxxxxxxx"); int offs

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

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;
        }
    }
}
破坏消息框后显示“发生一个或多个错误”。问题是什么?电报机器人应该回复:“但它不会。我无法确定m是否收到任何更新。

请尝试以下操作:

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

你没有执行任务。对我来说,最近在使bot.SendTextMessageAsync时发生了同样的错误(代码在2020年2月起作用,但后来不起作用),我收到了同样的异常。经过研究,我找到了这个话题[ 这实际上解决了我的问题

    using System.Net;

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

你应该给出更多的细节,而不仅仅是一行代码。解释一下这是如何解决问题的。看一看。我有一个机器人,我想在机器人上创建聊天模块,所以这是可能的?ypu能帮助我请更多关于你的问题的Ex-plane。。。