'Visual Studio 2019 does not see such things as .StartReceiving () and others | Telegram-bot

I'm trying to make a simple Telegram bot on C #. I installed NuGet packages such as Telegram.Bot and Telegram.Bot.Extensions.Polling. I also watched a few video tutorials.

However, I have errors with .StartReceiving(), .StopReceiving(), .OnMessage, MessageEventArgs., ReplyKeyboardMarkup and KeyboardButton.

And I don't know the reason for that

using System;
using System.Collections.Generic;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.ReplyMarkups;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;

namespace TelegramBot
{
    class Program
    {
        private static string Token { get; set; } = "My token";
        private static TelegramBotClient client;

        static void Main(string[] args)
        {
            client = new TelegramBotClient(Token);
            client.StartReceiving();
            client.OnMessage += OnMessageHandler;
            Console.ReadLine();
            client.StopReceiving();
        }

        private static async void OnMessageHandler(object sender, MessageEventArgs e)
        {
            var msg = e.Message;

            if (msg.Text != null)
            {
                Console.WriteLine($"Received a message with the text: {msg.Text}");
                switch (msg.Text)
                {
                    case "Sticke":
                        await client.SendStickerAsync(
                            chatId: msg.Chat.Id,
                            sticker: "Link for a sticker",
                            replyToMessageId: msg.MessageId,
                            replyMarkup: GetButtons());
                        break;

                    case "Sticker":
                        await client.SendPhotoAsync(
                            chatId: msg.Chat.Id,
                            photo: "Link for a pic",
                            replyMarkup: GetButtons());
                        break;

                    default:
                        await client.SendTextMessageAsync(msg.Chat.Id, "select a command: ", replyMarkup: GetButtons());
                        break;
                }
            }
        }

        private static IReplyMarkup GetButtons()
        {
            return new ReplyKeyboardMarkup
            {
                Keyboard = new List<List<KeyboardButton>>
                {
                    new List<KeyboardButton>{ new KeyboardButton { Text = "Sticker"}, new KeyboardButton { Text = "Pic"} },
                    new List<KeyboardButton>{ new KeyboardButton { Text = "123"}, new KeyboardButton { Text = "456"} }
                }
            };
        }
    }
}
}


Solution 1:[1]

So I know you probably watched a lot of tutorials about this and maybe those were a little outdated, but actually the way you handle messages has changed quite a bit.

Let me show how I do this:

    client.StartReceiving(async (bot, update, token) => {
          if (update.Message is Message message)
          {
             await client.SendTextMessageAsync(message.Chat, "hello world!");
          }
    });

In the code above I used a lambda expression, which is much more readable imo. But you can still use whatever way you prefer the most.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1