简体   繁体   中英

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"} }
                }
            };
        }
    }
}
}

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.

Thank you for the code, but i don't understand it have you a sample with the new Telegram.Bot 17.0?

I have also problem with OnMessage, OnCallbackQuery, StartReceiving, StopReceiving.

Everythings works with Telegram.Bot 16.0.2 but now with 17.0 nothings works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM