gpt4 book ai didi

c# - 如何使用 visual 2013 处理 telegrambot 中的这个错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:13 25 4
gpt4 key购买 nike

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

错误:

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

C# 2013 更新3网络表格 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);
}

}
}
}

错误:

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

1.gg.result`messages from bot的一条消息为null.如何不获取null message.

  1. 我想在用户发送任何消息时用 bot 发送消息,但我不能。

  2. 在此示例中如何使用 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"}}]}

最佳答案

试试下面的代码

            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。

关于c# - 如何使用 visual 2013 处理 telegrambot 中的这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com