-
-
Save MrChrisHammond/691a754b0dfbec1456cb328c3a4b36e2 to your computer and use it in GitHub Desktop.
TelegramGPTChatBot - Step 1
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
namespace TelegramChatBot | |
{ | |
internal class ChatBot | |
{ | |
public TelegramBotClient client { get; set; } | |
public string botName { get; set; } | |
public string fullName { get; set; } | |
public string personality { get; set; } | |
private string apiToken { get; set; } | |
public ChatBot(string apiToken, string botName = "BotName", string fullName = "Full Name", string personality = "A helpful and friendly robot.") | |
{ | |
this.apiToken = apiToken; | |
this.botName = botName; | |
this.fullName = fullName; | |
this.personality = personality; | |
StartTelegramBotClient(); | |
} | |
public async void StartTelegramBotClient() | |
{ | |
client = new TelegramBotClient(apiToken); | |
using CancellationTokenSource cts = new(); | |
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool. | |
ReceiverOptions receiverOptions = new() | |
{ | |
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types | |
}; | |
client.StartReceiving( | |
updateHandler: HandleUpdateAsync, | |
pollingErrorHandler: HandlePollingError, | |
receiverOptions: receiverOptions, | |
cancellationToken: cts.Token | |
); | |
var me = await client.GetMeAsync(); | |
Console.WriteLine($"Start listening for @{me.Username}"); | |
Console.ReadLine(); | |
// Send cancellation request to stop bot | |
cts.Cancel(); | |
} | |
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 reply to be added in next step. Keep reading. | |
} | |
private Task HandlePollingError(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) | |
{ | |
var ErrorMessage = exception switch | |
{ | |
ApiRequestException apiRequestException | |
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}", | |
_ => exception.ToString() | |
}; | |
Console.WriteLine(ErrorMessage); | |
return Task.CompletedTask; | |
} | |
} | |
} |
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
namespace TelegramChatBot | |
{ | |
internal class TelegramGPTBots | |
{ | |
private List<ChatBot> botClientList = new List<ChatBot>(); | |
public TelegramGPTBots() | |
{ | |
botClientList.Add(new ChatBot("telegramAPIKey", "insertTelegramBotName", "Susie Green", "You are Susie Green from Curb Your Enthusiasm. Your personality is filled with sarcasm, you are blunt, outspoken, strong-willed, and foul-mouthed.")); | |
botClientList.Add(new ChatBot("telegramAPIKey", "insertTelegramBotName", "Larry David", "You are Larry David from Curb Your Enthusiasm. Your personality is filled with Sardonic Humor, you are blunt, candid, and self-centered.")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment