Exemplo n.º 1
0
        /// <summary>
        /// This extension only use only <see cref="ITelegramBotClient"/>, no middleware, so it can't prevent handling by other middlewares in your pipeline.
        /// </summary>
        public static async Task <Message> ReadMessageAsync(
            this ITelegramBotClient bot,
            ChatId chatId,
            NoContextReadCallbackFromType validateType = NoContextReadCallbackFromType.AnyUser
            )
        {
            var tcs = new TaskCompletionSource <Message>(TaskCreationOptions.RunContinuationsAsynchronously);
            EventHandler <Args.MessageEventArgs> ev = null;

            ev = (s, e) =>
            {
                var isValid = ValidateMessage(bot, chatId, e.Message, validateType);
                if (isValid)
                {
                    bot.OnMessage -= ev;
                    tcs.TrySetResult(e.Message);
                }
            };
            bot.OnMessage += ev;
            return(await tcs.Task);
        }
Exemplo n.º 2
0
        static bool ValidateMessage(ITelegramBotClient bot, ChatId chatId, Message newMsg, NoContextReadCallbackFromType validateType)
        {
            try
            {
                if (newMsg.Chat.Id != chatId.Identifier)
                {
                    return(false);
                }

                if (validateType == NoContextReadCallbackFromType.AnyUserReply)
                {
                    if (newMsg.ReplyToMessage?.From.Id != bot.BotId)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }