How to handle this error in telegrambot with visua

2019-07-13 18:22发布

I want with bot telegram Response any message when The request comes from the user's side for bot by webform, but When the value of rs.message is null, Program error.

error:

An exception of type 'System.NullReferenceException' occurred in BotTelegramWeb.dll but was not handled in user code

C# 2013 update3 webform 2013 update3

Summary code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Helpers;
using System.IO;
using Newtonsoft.Json;
using System.Net;


namespace BotTelegramWeb
{
    public partial class TaktopBot : System.Web.UI.Page
    {
        //Api bot = new Api("Token");
        Telegram.Bot.TelegramBotClient Bot = new Telegram.Bot.TelegramBotClient("Token");


        public class mydata
        {
            public result[] result;
        }
        public class result
        {
            public int update_id { get; set; }
            public message message { get; set; }
        }
        public class message
        {
            public int message_id { get; set; }
            public message_from from { get; set; }
            public message_chat chat { get; set; }
            public int date { get; set; }
            public string text { get; set; }
        }
        public class message_from
        {
            public int ind { get; set; }
            public string first_name { get; set; }
            public string username { get; set; }
        }
        public class message_chat
        {
            public int id { get; set; }
            public string first_name { get; set; }
            public string username { get; set; }
        }




        public static void SendMessage(string chat_id, string message)
        {
            WebRequest req = WebRequest.Create("https://api.telegram.org/Token/sendMessage?chat_id=@BestLaptopBuyTest&text=123");
            req.UseDefaultCredentials = true;

            var result = req.GetResponse();
            req.Abort();
        }


        protected void Page_Load(object sender, EventArgs e)
        {



            WebRequest req = WebRequest.Create("https://api.telegram.org/bot" + "Token" + "/getUpdates");
            req.UseDefaultCredentials = true;
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            sr.Close();
            var jobject = Newtonsoft.Json.Linq.JObject.Parse(s);
            mydata gg = JsonConvert.DeserializeObject<mydata>(jobject.ToString());
            List<result> results = new List<result>();
            foreach (result rs in gg.result)
            {
                results.Add(rs);
                SendMessage(rs.message.chat.id.ToString(), "hello" + " " + "Dear" + rs.message.chat.first_name);
            }

        }   
    }
}

error:

An exception of type 'System.NullReferenceException' occurred in BotTelegramWeb.dll but was not handled in user code

1.one message of gg.result`messages from bot is null.How to not get null message.

  1. i want to send message with bot when user send any message but i can not.

  2. how to can use webhook method Instead update method in this example?

respone from : https://api.telegram.org/Token/getUpdates

{"ok":true,"result":[{"update_id":547758881,
"message":{"message_id":11,"from":{"id":301646351,"first_name":"s","last_name":"s"},"chat":{"id":301646351,"first_name":"s","last_name":"s","type":"private"},"date":1482667543,"text":"s"}},{"update_id":547758882,
"message":{"message_id":12,"from":{"id":301646351,"first_name":"s","last_name":"s"},"chat":{"id":301646351,"first_name":"s","last_name":"s","type":"private"},"date":1482667701,"text":"a"}},{"update_id":547758883,
"channel_post":{"message_id":26,"chat":{"id":-1001096443511,"title":"BestLaptopBuyTest","username":"BestLaptopBuyTest","type":"channel"},"date":1482671517,"text":"hello"}},{"update_id":547758884,
"message":{"message_id":21,"from":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1"},"chat":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1","type":"private"},"date":1482676503,"text":"s"}},{"update_id":547758885,
"message":{"message_id":45,"from":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1"},"chat":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1","type":"private"},"date":1482677210,"text":"ab"}},{"update_id":547758886,
"channel_post":{"message_id":112,"chat":{"id":-1001096443511,"title":"BestLaptopBuyTest","username":"BestLaptopBuyTest","type":"channel"},"date":1482741137,"text":"mohsen"}},{"update_id":547758887,
"message":{"message_id":144,"from":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1"},"chat":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1","type":"private"},"date":1482741149,"text":"mohsen"}},{"update_id":547758888,
"message":{"message_id":145,"from":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1"},"chat":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1","type":"private"},"date":1482741163,"text":"ma"}},{"update_id":547758889,
"message":{"message_id":146,"from":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1"},"chat":{"id":301646351,"first_name":"s","last_name":"s","username":"TestMohsen1","type":"private"},"date":1482747400,"text":"salam"}}]}

1条回答
ら.Afraid
2楼-- · 2019-07-13 19:03

Try the following code

            int Offset = 0 ;
            WebRequest req = WebRequest.Create("https://api.telegram.org/bot" + "Token" + "/getUpdates?offset=" + Offset;);
            req.UseDefaultCredentials = true;
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            sr.Close();
            var jobject = Newtonsoft.Json.Linq.JObject.Parse(s);
            mydata gg = JsonConvert.DeserializeObject<mydata>(jobject.ToString());
            List<result> results = new List<result>();
            foreach (result rs in gg.result)
            {
                results.Add(rs);
                SendMessage(rs.message.chat.id.ToString(), "hello" + " " + "Dear" + rs.message.chat.first_name);
            }

1.offset for only new Updates. 2.webhook used after local test. 3. for skip null , use and test callbackQuery.

查看更多
登录 后发表回答