X
X
xXNullXx2020-06-22 19:43:21
C++ / C#
xXNullXx, 2020-06-22 19:43:21

Telegram. How to handle user responses to a bot message?

Good day!

How to handle user responses to a bot message? That is, for example:
Bot:

Enter the name of the stock/s in the following format: "MOEX, AFLT"

User:
SBER, CHMF


And then what the user sent is stored in the sheet.
Tried like this (command class):
public class PredictCommand : Command
    {
        public override string Name => "predict";

        public override async Task Execute(Message message, ITelegramBotClient client)
        {
            var chatId = message.Chat.Id;

            string ms = null;
            var users = BufferUsers.GetInstance().Users;
            foreach (var i in users)
            {
                if (chatId == i.ChatId)
                {
                    ms = "Введите название акции/й в следующем формате: MOEX, AFLT";
                    break;
                }
                else
                {
                    ms = "Вы незарегестрированы! Воспользуйтесь командой /reg.  ";
                    return;
                }
            }

            await client.SendTextMessageAsync(chatId, ms, parseMode: ParseMode.Markdown, replyMarkup: null);

            GettingResponse(chatId).Wait();
        }

        public async Task GettingResponse(long chatId)
        {
            var client = Bot.GetInstance().Get();
            var bf = BufferUsers.GetInstance(); // Класс с листом где хранятся пользователи
            int offset = 0;
            int timeout = 0;

            while (true)
            {
                var updates = await client.GetUpdatesAsync(offset, timeout);

                foreach (var update in updates)
                {
                    var message = update.Message;

                    foreach (var i in bf.Users)
                    {
                        if (chatId == i.ChatId)
                        {
                            foreach (var j in message.Text.Split(','))
                            {
                                i.Securities.Add(new Security(j));
                            }

                            break;
                        }
                    }
                }
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-07-03
@xXNullXx

1. You seem to have a bug in the Execute method - the loop looks very strange. I would simplify it to

const string ms = "Введите название акции/й в следующем формате: MOEX, AFLT";
if(users.Any(user => user.ChatId != chatId)) return;

2. GettingResponse(chatId).Wait(); - it makes no sense to write Wait(), since you have an asynchronous method.
3.
while (true)
            {
                var updates = await client.GetUpdatesAsync(offset, timeout);

This is where trolling comes into play.
You need to rethink the architecture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question