示例#1
0
        /// <summary>
        /// Main Telegram Bot Task
        /// <para>Telegram messages are requested from the Telegram service and processed accordingly.
        /// Messages are filtered on text-messages only and passed to the BotHelper class for further dispatching</para>
        /// </summary>
        /// <returns></returns>
        static async Task Run()
        {
            BotHelper BotHelper  = new BotHelper();
            User      currentBot = await BotHelper.GetBot();

            Config.Log($"Bot: {currentBot.Username} - Status Online...");

            var offset = 0;

            while (true)
            {
                try
                {
                    var updates = await BotHelper.GetUpdates(offset);

                    foreach (var update in updates)
                    {
                        if (update.Message.Type == MessageType.TextMessage)
                        {
                            string username = update.Message.Chat.FirstName;
                            if (update.Message.Chat.Type == ChatType.Group)
                            {
                                username = update.Message.From.FirstName;
                                Config.Log($"Group: {update.Message.Chat.Title} User: {username}  Message: {update.Message.Text}");
                            }
                            else
                            {
                                Config.Log($"User: {username} Message: {update.Message.Text}");
                            }

                            await BotHelper.ProcessCommand(update.Message.Chat.Id, update.Message.Text, username);
                        }
                        offset = update.Id + 1;
                    }

                    await Task.Delay(1000);
                }
                catch (Exception ex)
                {
                    Config.Log("Process stopped!");
                    Config.Log($"Error Message: {ex.Message}");
                    Restart();
                }
            }
        }