[英]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 #.我正在尝试在 C # 上制作一个简单的 Telegram 机器人。 I installed NuGet packages such as Telegram.Bot
and Telegram.Bot.Extensions.Polling
.我安装了 NuGet 包,例如Telegram.Bot
和Telegram.Bot.Extensions.Polling
。 I also watched a few video tutorials.我还看了一些视频教程。
However, I have errors with .StartReceiving()
, .StopReceiving()
, .OnMessage
, MessageEventArgs.
但是,我对.StartReceiving()
、 .StopReceiving()
、 .OnMessage
、 MessageEventArgs.
, ReplyKeyboardMarkup
and KeyboardButton
. 、 ReplyKeyboardMarkup
和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.在上面的代码中,我使用了 lambda 表达式,这在 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?谢谢你的代码,但我不明白你有新的 Telegram.Bot 17.0 的样本吗?
I have also problem with OnMessage, OnCallbackQuery, StartReceiving, StopReceiving.我对 OnMessage、OnCallbackQuery、StartReceiving、StopReceiving 也有问题。
Everythings works with Telegram.Bot 16.0.2 but now with 17.0 nothings works. Everythings 与 Telegram.Bot 16.0.2 一起工作,但现在与 17.0 无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.