我希望机器人电报响应任何消息当请求来自webform的用户方面,但当rs.message的值为null时,程序错误。
错误:
类型' System.NullReferenceException'的例外情况发生在BotTelegramWeb.dll中但未在用户代码中处理
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);
}
}
}
}
错误:
类型&#39; System.NullReferenceException&#39;的例外情况发生在BotTelegramWeb.dll中但未在用户代码中处理
1.来自bot的gg.result`消息的一条消息为null。如何得不到空消息。
我希望在用户发送任何消息但我不能发送消息时发送消息。
如何使用webhook方法在本例中更新方法?
来自: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"}}]}
答案 0 :(得分:1)
尝试以下代码
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.仅针对新更新的偏移。 2.局部测试后使用的网钩。 3.对于skip null,使用并测试callbackQuery。