L
L
LasonD2020-08-09 23:21:01
Bots
LasonD, 2020-08-09 23:21:01

Bot for Telegram does not send InlineKeyboardMarkup keyboard. The first few times he sent, then stopped. Why is that?

I am learning how to write a bot for Telegram. I ran into a problem: I created a method in order to find out exchange rates, the user must enter a date in the YYYY.MM.DD format, then an api request is sent, the JSON file is parsed into an array of entities, on the basis of which InlineButton.WithCallbackData buttons are then formed. Then I send all this to the user's chat:
bot.SendTextMessageAsync(e.Message.From.Id, "Select the currency you want to know the rate of.", replyMarkup: new InlineKeyboardMarkup(buttons));
buttons is a jagged array of InlineButton.WithCallbackData.
But these buttons are either sent (and everything works fine) several times in a row, or they are not sent even once, although I went through the debugger: everything works as it should and the program reaches bot.SendTextMessageAsync, but the buttons may be sent, they may not be sent.
Here is the code from the method.

var date = e.Message.Text;
string url = $"https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date={date}&json";

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                string response;
                using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    response = streamReader.ReadToEnd();
                }

                currencyResponse.Currencies = JsonConvert.DeserializeObject<Currency[]>(response);
                Currency[] currencies = currencyResponse.Currencies;

                InlineKeyboardButton[][] buttons = new InlineKeyboardButton[currencies.Length /  4][];
                int count = 0;
                for (int i = 0; i < currencies.Length / 4; i++)
                {
                    buttons[i] = new InlineKeyboardButton[currencies.Length / (currencies.Length / 4)];
                    for (int j = 0; j < buttons[i].Length; j++)
                    {
                        buttons[i][j] = InlineKeyboardButton.WithCallbackData($"{currencies[count].txt} {currencies[count].cc}");
                        count++;
                    }
                }

                bot.SendTextMessageAsync(e.Message.From.Id, "Оберіть валюту, курс якої хочете дізнатись.", replyMarkup: new InlineKeyboardMarkup(buttons)); // Здесь пользователю должна отослаться клавиатура для выбора

Photo of correct processing
5f305a6d72788730599910.png
I forgot to add that the link is working, moreover, if, for example, I have already received a keyboard, then if I write a new date, but I poke at the old keyboard, then info comes about a new date. That is, everything is parsed, the problem is that the keyboard does not come.
I will also add that I am not strong in asynchronous programming, but I tried this and that - it did not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
falsescam, 2020-08-10
@falsescam

You have a trouble with a link, if it’s easier, then the stat is not updated xs, it may be buggy and does not return the necessary data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question