c# - with - microsoft bot framework tutorial



¿Cómo manejar este error en telegrambot con visual 2013? (1)

Quiero con el telegrama bot Responder cualquier mensaje cuando La solicitud proviene del lado del usuario para bot por formulario web, pero cuando el valor de rs.message es nulo, Error de programa.

error:

Se produjo una excepción de tipo 'System.NullReferenceException' en BotTelegramWeb.dll pero no se manejó en el código de usuario

C # 2013 actualización3 webform 2013 actualización3

Código de resumen:

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/[email protected]&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:

Se produjo una excepción de tipo 'System.NullReferenceException' en BotTelegramWeb.dll pero no se manejó en el código de usuario

1.un mensaje de gg.result`mensajes de bot es nulo.Cómo no obtener un mensaje nulo.

  1. Quiero enviar un mensaje con bot cuando el usuario envía un mensaje, pero no puedo.

  2. ¿Cómo se puede utilizar el método webhook En lugar de actualizar el método en este ejemplo?

responde de: 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"}}]}

Pruebe el siguiente código

            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 para solo nuevas actualizaciones. 2.webhook utilizado después de la prueba local. 3. para omitir nulo, use y pruebe callbackQuery.





telegram-bot