Skip to content

Instantly share code, notes, and snippets.

@MrChrisHammond
Created October 13, 2023 16:51
Show Gist options
  • Save MrChrisHammond/dd8e8eed090493c4a9c0f8465e7242fe to your computer and use it in GitHub Desktop.
Save MrChrisHammond/dd8e8eed090493c4a9c0f8465e7242fe to your computer and use it in GitHub Desktop.
HandleUpdateAsync method within ChatBot.cs
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
if (update.Message is not { } message)
return;
if (message.Text is not { } messageText)
return;
DateTime targetDate = update.Message.Date;
TimeSpan timeDiff = DateTime.UtcNow - targetDate;
//no stale messages
if (Math.Abs(timeDiff.TotalSeconds) >= 30)
return;
ChatGPT chatGPT = new ChatGPT();
GPTResponse gptResponse = new GPTResponse();
if (((message.Text.Contains("@"+ botName)) ||
(message.ReplyToMessage != null && message.ReplyToMessage.From.Username == botName)) &&
botClient.BotId == client.BotId)
{
string query = messageText.Replace("@"+ botName, "");
gptResponse = await chatGPT.GenerateResponse($"{personality}. Reply to the following message: {query}");
var chatId = message.Chat.Id;
Telegram.Bot.Types.Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
gptResponse.response,
cancellationToken: cancellationToken,
replyToMessageId: message.MessageId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment