public TelegramService(string apiKey, ISession session) { this.bot = new TelegramBotClient(apiKey); this.session = session; var me = bot.GetMeAsync().Result; bot.OnMessage += OnTelegramMessageReceived; bot.StartReceiving(); this.session.EventDispatcher.Send(new NoticeEvent {Message = "Using TelegramAPI with " + me.Username}); }
public TelegramService(string apiKey, ISession session) { try { _bot = new TelegramBotClient(apiKey); _session = session; var me = _bot.GetMeAsync().Result; _bot.OnMessage += OnTelegramMessageReceived; _bot.StartReceiving(); _session.EventDispatcher.Send(new NoticeEvent {Message = "Using TelegramAPI with " + me.Username}); } catch (Exception) { _session.EventDispatcher.Send(new ErrorEvent { Message = "Unkown Telegram Error occured. "}); } }
internal void GetUpdates() { _client = new Telegram.Bot.TelegramBotClient(_token); var me = _client.GetMeAsync().Result; if (me != null && !string.IsNullOrEmpty(me.Username)) { int offset = 0; while (true) { try { var updates = _client.GetUpdatesAsync(offset).Result; if (updates != null && updates.Count() > 0) { foreach (var update in updates) { processUpdate(update); offset = update.Id + 1; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } //Thread.Sleep(1000); Task.Delay(1000); } } }
private async void telegramAPIAsync() { var Bot = new Telegram.Bot.TelegramBotClient("Token key"); var me = await Bot.GetMeAsync(); //Debug.WriteLine("Hello my name is {0}", me.FirstName); }
private static bool isInternetConnected() { try { Telegram.Bot.TelegramBotClient teleBot = new Telegram.Bot.TelegramBotClient(botToken); var me = teleBot.GetMeAsync().Result; return(true); } catch { return(false); } }
static void Main(string[] args) { //bot.loadSettings(); bot.OnMessage += BotOnMessage; bot.SetWebhookAsync(); var me = bot.GetMeAsync().Result; Console.Title = me.Username; bot.StartReceiving(); Console.ReadLine(); bot.StopReceiving(); }
public TelegramConnector() { botClient = new Telegram.Bot.TelegramBotClient("524727579:AAHZaFNr3S1vUfuPy-wZEIGO3-WxQWodL5s"); var me = botClient.GetMeAsync(); Console.WriteLine("Api for bot: " + me.Result.FirstName + " - was connected"); Console.WriteLine("Connected to: " + botClient.GetChatAsync(chatId).Result.Title + "chat"); botClient.OnMessage += onMessage; botClient.StartReceiving(Array.Empty <UpdateType>()); Thread.Sleep(2000); }
public async void testApi() //метод проверки связи данного бота по нашему токену { try { Bot = new Telegram.Bot.TelegramBotClient(token); var me = await Bot.GetMeAsync(); ApiCheck = 0; //Thread newThread = new Thread(RemindBookBotClient.ReceiveMessage); //newThread.Start(); } catch (Exception ex) { ApiCheck = 1; } }
public async void testApiAsync() { Bot = new Telegram.Bot.TelegramBotClient("349066899:AAFiqkKRiwbO8Rgc1bLdANgLqWpbiJvfK3U"); var me = await Bot.GetMeAsync(); System.Console.WriteLine("Hello my name is " + me.FirstName); while (true) { try { var updates = await GetUpdatesAsync(); foreach (var update in updates) { if (updateDictionary.ContainsKey(update.Message.Chat.Id)) { updateDictionary[update.Message.Chat.Id] = update; } else { updateDictionary.Add(update.Message.Chat.Id, update); } _offset = update.Id + 1; } foreach (var key in updateDictionary) { if (key.Value.Message.Text != null && key.Value.Message.Text != String.Empty) { Console.WriteLine(updates.Length); } OnMessageReceived?.Invoke(key.Value.Message); } updateDictionary = new Dictionary <long, Update>(); } catch (Exception) { Thread.Sleep(10000); } } }
static void Main(string[] args) { // 透過SendTextMessageAsync來傳送訊息,傳入chanelid及要傳送的訊息 //chanelId test Bot.SendTextMessageAsync(chanelId, "每5秒傳送一個訊息"); Bot.SendTextMessageAsync(chanelId, "Mickey Mouse"); var me = Bot.GetMeAsync().Result; Console.Title = me.Username; Bot.OnMessage += BotOnMessageEcho; Bot.OnMessageEdited += BotOnMessageEcho; /* Bot.OnCallbackQuery += BotOnCallbackQueryReceived; * Bot.OnReceiveError += BotOnReceiveError;*/ Bot.StartReceiving(Array.Empty <UpdateType>()); Console.WriteLine($"Start listening for @cutebaby0630_Bot"); Console.ReadLine(); Bot.StopReceiving(); }
static async Task MainLoop() { // Read Configuration var telegramKey = ConfigurationManager.AppSettings["TelegramKey"]; // Start Bot var bot = new TelegramBotClient(telegramKey); var me = await bot.GetMeAsync(); Console.WriteLine(me.Username + " started at " + DateTime.Now); var offset = 0; while (true) { var updates = new Update[0]; try { updates = await bot.GetUpdatesAsync(offset); } catch (TaskCanceledException) { // Don't care } catch (Exception ex) { Console.WriteLine("ERROR WHILE GETTIGN UPDATES - " + ex); } foreach (var update in updates) { offset = update.Id + 1; ProcessUpdate(bot, update, me); } await Task.Delay(1000); } }