Тема: Потрібно виправити помилку в боті
Доброго дня, потрібно виправити помилку в боті: Введена стрічка має не корректний формат(Input string was not in a correct format)
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types.ReplyMarkups;
using System.Text.RegularExpressions;
using System.Net;
namespace TelegramBotExperiments
{
class Program
{
static ITelegramBotClient bot = new TelegramBotClient("Мій токен");
public static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(update));
if (update.Type == Telegram.Bot.Types.Enums.UpdateType.Message)
{
var message = update.Message;
if (message.Text.ToLower() == "/start")
{
await botClient.SendTextMessageAsync(message.Chat, "Привіт!");
return;
}
if(message.Text.ToLower().Contains("/api"))
{
try
{
Match latitude;
Match longitude;
string information;
Match match = Regex.Match(message.Text, "/api (.*?)");
using (WebClient wc = new WebClient())
information = wc.DownloadString($"http://free.ipwhois.io/xml/{match.Groups[1].Value}");
latitude = Regex.Match(information, "<latitude>(.*?)</latitude>");
longitude = Regex.Match(information, "<longitude>(.*?)</longitude>");
await botClient.SendLocationAsync(
chatId: message.Chat.Id,
longitude: Convert.ToDouble(longitude.Groups[1].Value),
latitude: Convert.ToDouble(latitude.Groups[1].Value),
cancellationToken: cancellationToken);
}
catch(Exception e)
{
await botClient.SendTextMessageAsync(message.Chat, e.Message);
}
}
}
}
public static async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(exception));
}
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
var cancellationToken = cts.Token;
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { },
};
bot.StartReceiving(
HandleUpdateAsync,
HandleErrorAsync,
receiverOptions,
cancellationToken
);
Console.ReadLine();
}
}
}