如何在 C# 的电报 Bot 中单击 InlineKeyboardCallbackButton 时获取当前的 CallbackData
How Can Get CallbackData in current when InlineKeyboardCallbackButton clicked in telegram Bot in C#
如何在电报机器人中点击 InlineKeyboardCallbackButton?
这是我的代码:我编辑了代码
public async System.Threading.Tasks.Task<ActionResult> GetMsgAsync()
{
var req = Request.InputStream;
var responsString = new StreamReader(req).ReadToEnd();
var update = JsonConvert.DeserializeObject<Update>(responsString);
var message = update.Message;
var chat = message.Chat;
InlineKeyboardMarkup categoryInlineMarkup = new
InlineKeyboardMarkup(
new InlineKeyboardButton[][]
{
new InlineKeyboardButton[]
{
new InlineKeyboardCallbackButton("button1","callbackData")
}
}
);
await api.SendTextMessageAsync(update.Message.Chat.Id, "Please click the button", replyMarkup: categoryInlineMarkup);
if (update.Type == Telegram.Bot.Types.Enums.UpdateType.MessageUpdate)
{
// all codes just run in this block
}
if (update.Type == Telegram.Bot.Types.Enums.UpdateType.CallbackQueryUpdate)
{
// I can't get clicked button here
if (update.CallbackQuery.Data.Contains("callbackData"))
{
await api.AnswerCallbackQueryAsync(update.CallbackQuery.Id, update.CallbackQuery.Data);
}
}
}
如何在 webhook 方法中而不是在控制台程序中单击此按钮?
终于找到答案了,问题是这行代码:
var message = update.Message;
this line of code is the main problem because this line is not in try
catch block and i can't understand which line is the problem .
在这个问题之后,我有一个建议给你
您可以在您的个人计算机上调试您的电报机器人,而无需在任何主机上上传任何代码,为此您可以 运行 以下 3 个步骤 ::
1- Accessing an IIS Express site from a remote computer
3- 下载 ngrok 后,您可以将电报 webhook 请求转发到您的个人计算机。
如何在电报机器人中点击 InlineKeyboardCallbackButton?
这是我的代码:我编辑了代码
public async System.Threading.Tasks.Task<ActionResult> GetMsgAsync()
{
var req = Request.InputStream;
var responsString = new StreamReader(req).ReadToEnd();
var update = JsonConvert.DeserializeObject<Update>(responsString);
var message = update.Message;
var chat = message.Chat;
InlineKeyboardMarkup categoryInlineMarkup = new
InlineKeyboardMarkup(
new InlineKeyboardButton[][]
{
new InlineKeyboardButton[]
{
new InlineKeyboardCallbackButton("button1","callbackData")
}
}
);
await api.SendTextMessageAsync(update.Message.Chat.Id, "Please click the button", replyMarkup: categoryInlineMarkup);
if (update.Type == Telegram.Bot.Types.Enums.UpdateType.MessageUpdate)
{
// all codes just run in this block
}
if (update.Type == Telegram.Bot.Types.Enums.UpdateType.CallbackQueryUpdate)
{
// I can't get clicked button here
if (update.CallbackQuery.Data.Contains("callbackData"))
{
await api.AnswerCallbackQueryAsync(update.CallbackQuery.Id, update.CallbackQuery.Data);
}
}
}
如何在 webhook 方法中而不是在控制台程序中单击此按钮?
终于找到答案了,问题是这行代码:
var message = update.Message;
this line of code is the main problem because this line is not in try catch block and i can't understand which line is the problem .
在这个问题之后,我有一个建议给你
您可以在您的个人计算机上调试您的电报机器人,而无需在任何主机上上传任何代码,为此您可以 运行 以下 3 个步骤 ::
1- Accessing an IIS Express site from a remote computer
3- 下载 ngrok 后,您可以将电报 webhook 请求转发到您的个人计算机。