Last active
February 23, 2019 22:51
-
-
Save MihaZupan/4a30d90c6223eb07ea272e804010c07a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Telegram.Bot; | |
using Telegram.Bot.Exceptions; | |
using Telegram.Bot.Extensions.Polling; | |
using Telegram.Bot.Types; | |
using Telegram.Bot.Types.Enums; | |
namespace AsyncBot | |
{ | |
class Program | |
{ | |
public static TelegramBotClient Bot = new TelegramBotClient("123:abc"); | |
public static async Task Main() | |
{ | |
await Bot.ReceiveAsync(new DefaultUpdateHandler(HandleUpdateAsync, HandleExceptionAsync)); | |
// OR | |
await Bot.ReceiveAsync<CustomUpdateHandler>(); | |
} | |
public class CustomUpdateHandler : IUpdateHandler | |
{ | |
public UpdateType[]? AllowedUpdates => null; | |
public Task ErrorOccurred(Exception exception) | |
{ | |
return HandleExceptionAsync(exception); | |
} | |
public Task UpdateReceived(Update update) | |
{ | |
return HandleUpdateAsync(update); | |
} | |
} | |
public static async Task HandleUpdateAsync(Update update) | |
{ | |
if (update.Message is Message message && message.Text is string text) | |
{ | |
await Bot.SendTextMessageAsync(message.Chat, "Echo:\n" + text); | |
} | |
} | |
public static async Task HandleExceptionAsync(Exception exception) | |
{ | |
if (exception is ApiRequestException apiRequestException) | |
{ | |
Console.WriteLine(apiRequestException.ToString()); | |
} | |
else | |
{ | |
await Bot.SendTextMessageAsync(123, "Something went wrong:\n" + exception.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment