如何使用visual 2013处理telegrambot中的这个错误? [英] How to handle this error in telegrambot with visual 2013?

查看:34
本文介绍了如何使用visual 2013处理telegrambot中的这个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当请求来自用户端对 bot 的 webform 请求时,我希望使用机器人电报响应任何消息,但是当 rs.message 的值为 null 时,程序错误.

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.

错误:

BotTelegramWeb.dll 中发生了System.NullReferenceException"类型的异常,但未在用户代码中处理

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

C# 2013 update3网络表单 2013 更新 3

C# 2013 update3 webform 2013 update3

摘要代码:

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);
            }

        }   
    }
}

错误:

BotTelegramWeb.dll 中发生了System.NullReferenceException"类型的异常,但未在用户代码中处理

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

1. 来自bot的gg.result`消息的一条消息为空.如何不获取空消息.

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

  1. 我想在用户发送任何消息时使用机器人发送消息,但我不能.

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

在这个例子中如何使用 webhook 方法代替 update 方法?

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

来自: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"}}]}

推荐答案

试试下面的代码

            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 仅用于新更新.2.webhook 本地测试后使用.3. 跳过 null ,使用并测试 callbackQuery.

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

这篇关于如何使用visual 2013处理telegrambot中的这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆