-
-
Save MrChrisHammond/dd8e8eed090493c4a9c0f8465e7242fe to your computer and use it in GitHub Desktop.
HandleUpdateAsync method within ChatBot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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