Skip to content

Instantly share code, notes, and snippets.

@MrChrisHammond
Last active October 13, 2023 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrChrisHammond/691a754b0dfbec1456cb328c3a4b36e2 to your computer and use it in GitHub Desktop.
Save MrChrisHammond/691a754b0dfbec1456cb328c3a4b36e2 to your computer and use it in GitHub Desktop.
TelegramGPTChatBot - Step 1
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;
}
}
}
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