public class TelegramBotController : ControllerBase
{
private readonly TelegramBotClient _botClient;
private const string BotToken = "MyToken";
public TelegramBotController()
{
_botClient = new TelegramBotClient(BotToken);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] Telegram.Bot.Types.Update update)
{
await _botClient.SendTextMessageAsync(chatId: myUserId, "Hello guys");
return Ok();
}
}
这是我从机器人电报接收回调的代码,我还使用邮递员成功调用
setwebhook
。
当我向电报机器人发送文本时,它没有收到回调。
我正在关注这个视频:
https://www.youtube.com/watch?v=-Er2kaKxaBg
我尝试搜索并使用 chatgpt 寻求帮助,但它不起作用。我的期望是接收来自电报的回调并将其发送回消息
要接收回电,您需要设置一个
webhook
URL,以便 Telegram 在状态发生变化时给您回电。
参考以下代码:
public class TelegramBotController : ControllerBase
{
private readonly TelegramBotClient _botClient;
private const string BotToken = "YourBotToken";
public TelegramBotController()
{
_botClient = new TelegramBotClient(BotToken);
}
[HttpPost("/webhook")] // Set up your webhook URL here
public async Task<IActionResult> Post([FromBody] Update update)
{
if (update == null)
{
return BadRequest();
}
if (update.Message != null)
{
await HandleIncomingMessage(update.Message); // For incoming messages
}
else if (update.CallbackQuery != null)
{
await HandleIncomingCallbackQuery(update.CallbackQuery); // For incoming callback queries
}
return Ok();
}
private async Task HandleIncomingMessage(Message message)
{
await _botClient.SendTextMessageAsync(chatId: message.Chat.Id, text: "Hello guys"); // Your message handling logic here
}
private async Task HandleIncomingCallbackQuery(CallbackQuery callbackQuery)
{
Console.WriteLine("Call back query Incomming");
}
}