c#电报bot令牌处理问题



我正在编写一个小型电报机器人,它可以从维基百科中提取文章。我将令牌作为字符串传递。但是当尝试处理令牌时,出现错误:

Newtonsoft.Json。JsonReaderException: "JSON整数6076351611对于Int32来说太大或太小。路径的结果。id',第一行,位置36.">

代码:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums;
namespace WikiBot
{
class Program
{
private static readonly HttpClient client = new HttpClient();
private static readonly Random random = new Random();
static async Task Main()
{
var botClient = new TelegramBotClient("6076351611:AAHOpSdOQl0Mi6WUGFV_otVZH_M81IdplvM");
var me = await botClient.GetMeAsync();
Console.WriteLine($"Hello, my name is {me.FirstName}!");
botClient.OnMessage += Bot_OnMessage;
botClient.StartReceiving();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
botClient.StopReceiving();
}
private static async void Bot_OnMessage(object sender, MessageEventArgs update)
{
if (update.Message.Type != MessageType.Text)
return;
if (update.Message.Text == "/wiki")
{
var articleTitle = await GetRandomArticleTitleAsync();
if (!string.IsNullOrEmpty(articleTitle))
{
var articleUrl = $"https://en.wikipedia.org/wiki/{articleTitle.Replace(" ", "_")}";
await ((TelegramBotClient)sender).SendTextMessageAsync(
chatId: update.Message.Chat,
text: $"Here's a random Wikipedia article for you: {articleUrl}",
disableWebPagePreview: true);
}
}
}
private static async Task<string> GetRandomArticleTitleAsync()
{
var response = await client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=random&rnlimit=1&format=json");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var result = JObject.Parse(json)["query"]["random"].ToObject<List<JObject>>()[0];
return result["title"].ToString();
}
}
}

截图如何修复这个错误?我理解为什么会这样,但由于缺乏经验,我无法克服。

尝试使用16.0.2版本的Telegram。Bot NuGet包,对我有用。在这个版本之后,有新的方法来做这些事情。

相关内容

  • 没有找到相关文章

最新更新