繁体   English   中英

Visual Studio 2019 看不到.StartReceiving() 等 | 电报机器人

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

我正在尝试在 C # 上制作一个简单的 Telegram 机器人。 我安装了 NuGet 包,例如Telegram.BotTelegram.Bot.Extensions.Polling 我还看了一些视频教程。

但是,我对.StartReceiving().StopReceiving().OnMessageMessageEventArgs. ReplyKeyboardMarkupKeyboardButton

我不知道这是什么原因

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

所以我知道你可能看过很多关于这个的教程,也许这些教程有点过时了,但实际上你处理消息的方式已经发生了很大变化。

让我展示一下我是如何做到这一点的:

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

在上面的代码中,我使用了 lambda 表达式,这在 imo 中更具可读性。 但是您仍然可以使用您最喜欢的任何方式。

谢谢你的代码,但我不明白你有新的 Telegram.Bot 17.0 的样本吗?

我对 OnMessage、OnCallbackQuery、StartReceiving、StopReceiving 也有问题。

Everythings 与 Telegram.Bot 16.0.2 一起工作,但现在与 17.0 无关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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