mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-03-02 00:14:34 +00:00
Created user-role system
This commit is contained in:
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# Go vendor directory
|
||||
vendor/
|
||||
|
||||
# Environment variables
|
||||
.env
|
||||
|
||||
# Log file
|
||||
bot.log
|
||||
|
||||
# Database file
|
||||
bot.db
|
||||
228
bot.log
Normal file
228
bot.log
Normal file
@@ -0,0 +1,228 @@
|
||||
2024/10/13 01:01:54 Error initializing Telegram bot: TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
2024/10/13 01:02:03 Error initializing Telegram bot: TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
2024/10/13 01:05:42 TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
2024/10/13 01:05:42 Error initializing Telegram bot: TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
|
||||
2024/10/13 01:09:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.030ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:09:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.063ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:09:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.024ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:09:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.026ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
2024/10/13 01:09:23 TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
2024/10/13 01:09:23 Error initializing Telegram bot: TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
|
||||
2024/10/13 01:11:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.049ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:11:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.109ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:11:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.035ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:11:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:95
|
||||
[0.027ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
2024/10/13 01:11:10 TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
2024/10/13 01:11:10 Error initializing Telegram bot: TELEGRAM_BOT_TOKEN environment variable is not set
|
||||
|
||||
2024/10/13 01:14:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:96
|
||||
[0.034ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:14:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:96
|
||||
[0.091ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:14:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:96
|
||||
[0.032ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:14:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:96
|
||||
[0.026ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
2024/10/13 01:14:02 Telegram bot initialized successfully
|
||||
2024/10/13 01:14:02 Starting bot...
|
||||
|
||||
2024/10/13 01:14:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:139
|
||||
[0.347ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:14:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:160
|
||||
[29.627ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:14:22.558","2024-10-13 01:14:22.558",NULL,1404948412,1404948412,"tibikgaming","/start","2024-10-13 01:14:22.558") RETURNING `id`
|
||||
|
||||
2024/10/13 01:14:30 /home/fedora/Desktop/thatsky-telegram-bot/main.go:139
|
||||
[0.368ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:14:30 /home/fedora/Desktop/thatsky-telegram-bot/main.go:160
|
||||
[31.078ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:14:30.954","2024-10-13 01:14:30.954",NULL,1404948412,1404948412,"tibikgaming","ejeje","2024-10-13 01:14:30.953") RETURNING `id`
|
||||
|
||||
2024/10/13 01:14:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:139
|
||||
[0.271ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:14:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:160
|
||||
[28.877ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:14:33.717","2024-10-13 01:14:33.717",NULL,1404948412,1404948412,"tibikgaming","wwwl","2024-10-13 01:14:33.717") RETURNING `id`
|
||||
|
||||
2024/10/13 01:15:03 /home/fedora/Desktop/thatsky-telegram-bot/main.go:139
|
||||
[0.337ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:15:03 /home/fedora/Desktop/thatsky-telegram-bot/main.go:160
|
||||
[29.282ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:15:03.957","2024-10-13 01:15:03.957",NULL,1404948412,1404948412,"tibikgaming","/stop","2024-10-13 01:15:03.957") RETURNING `id`
|
||||
|
||||
2024/10/13 01:16:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:139
|
||||
[0.328ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:16:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:160
|
||||
[63.169ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:16:24.604","2024-10-13 01:16:24.604",NULL,1404948412,1404948412,"tibikgaming","/start","2024-10-13 01:16:24.604") RETURNING `id`
|
||||
|
||||
2024/10/13 01:22:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:107
|
||||
[0.023ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:22:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:107
|
||||
[0.057ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:22:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:107
|
||||
[0.017ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:22:33 /home/fedora/Desktop/thatsky-telegram-bot/main.go:107
|
||||
[0.014ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
2024/10/13 01:22:33 Telegram bot initialized successfully
|
||||
2024/10/13 01:22:33 Starting bot...
|
||||
|
||||
2024/10/13 01:22:40 /home/fedora/Desktop/thatsky-telegram-bot/main.go:150
|
||||
[0.415ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
2024/10/13 01:22:41 Error getting Anthropic response: error creating Anthropic message: error, status code: 401, message: anthropic api error type: authentication_error, message: x-api-key header is required
|
||||
|
||||
2024/10/13 01:22:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:170
|
||||
[29.341ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:22:41.244","2024-10-13 01:22:41.244",NULL,1404948412,1404948412,"tibikgaming","whaha","2024-10-13 01:22:41.244") RETURNING `id`
|
||||
2024/10/13 01:23:53 ANTHROPIC_API_KEY environment variable is not set
|
||||
|
||||
2024/10/13 01:24:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:115
|
||||
[0.029ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:24:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:115
|
||||
[0.087ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:24:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:115
|
||||
[0.026ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:24:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:115
|
||||
[0.025ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
2024/10/13 01:24:32 Telegram bot initialized successfully
|
||||
2024/10/13 01:24:32 Starting bot...
|
||||
|
||||
2024/10/13 01:24:37 /home/fedora/Desktop/thatsky-telegram-bot/main.go:158
|
||||
[0.452ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:24:37 /home/fedora/Desktop/thatsky-telegram-bot/main.go:178
|
||||
[29.297ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:24:37.878","2024-10-13 01:24:37.878",NULL,1404948412,1404948412,"tibikgaming","Hello","2024-10-13 01:24:37.878") RETURNING `id`
|
||||
|
||||
2024/10/13 01:24:47 /home/fedora/Desktop/thatsky-telegram-bot/main.go:158
|
||||
[0.363ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:24:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:178
|
||||
[29.210ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:24:48.283","2024-10-13 01:24:48.283",NULL,1404948412,1404948412,"tibikgaming","What can you do?","2024-10-13 01:24:48.283") RETURNING `id`
|
||||
|
||||
2024/10/13 01:25:30 /home/fedora/Desktop/thatsky-telegram-bot/main.go:158
|
||||
[0.275ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:25:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:178
|
||||
[29.287ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:25:31.024","2024-10-13 01:25:31.024",NULL,1404948412,1404948412,"tibikgaming","/start","2024-10-13 01:25:31.024") RETURNING `id`
|
||||
|
||||
2024/10/13 01:25:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:158
|
||||
[0.365ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:25:50 /home/fedora/Desktop/thatsky-telegram-bot/main.go:178
|
||||
[30.345ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`text`,`timestamp`) VALUES ("2024-10-13 01:25:50.786","2024-10-13 01:25:50.786",NULL,1404948412,1404948412,"tibikgaming","/start","2024-10-13 01:25:50.786") RETURNING `id`
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[0.031ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[0.099ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[0.027ms] [rows:-] SELECT * FROM `messages` LIMIT 1
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[29.214ms] [rows:0] ALTER TABLE `messages` ADD `user_role` text
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[0.085ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[0.023ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[20.029ms] [rows:0] CREATE TABLE `users` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`telegram_id` integer,`username` text,`role` text)
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[20.193ms] [rows:0] CREATE UNIQUE INDEX `idx_users_telegram_id` ON `users`(`telegram_id`)
|
||||
|
||||
2024/10/13 01:34:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124
|
||||
[20.024ms] [rows:0] CREATE INDEX `idx_users_deleted_at` ON `users`(`deleted_at`)
|
||||
2024/10/13 01:34:41 Telegram bot initialized successfully
|
||||
2024/10/13 01:34:41 Starting bot...
|
||||
|
||||
2024/10/13 01:34:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:167 record not found
|
||||
[0.273ms] [rows:0] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:34:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:174
|
||||
[29.703ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`telegram_id`,`username`,`role`) VALUES ("2024-10-13 01:34:48.336","2024-10-13 01:34:48.336",NULL,1404948412,"tibikgaming","user") RETURNING `id`
|
||||
|
||||
2024/10/13 01:34:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
|
||||
[0.257ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:34:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:227
|
||||
[0.208ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:34:48 /home/fedora/Desktop/thatsky-telegram-bot/main.go:204
|
||||
[29.277ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`) VALUES ("2024-10-13 01:34:48.901","2024-10-13 01:34:48.901",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 01:34:48.901") RETURNING `id`
|
||||
|
||||
2024/10/13 01:34:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:167
|
||||
[0.370ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:34:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
|
||||
[0.099ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:34:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:227
|
||||
[0.072ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:34:57 /home/fedora/Desktop/thatsky-telegram-bot/main.go:204
|
||||
[29.572ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`) VALUES ("2024-10-13 01:34:56.99","2024-10-13 01:34:56.99",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 01:34:56.99") RETURNING `id`
|
||||
|
||||
2024/10/13 01:35:20 /home/fedora/Desktop/thatsky-telegram-bot/main.go:167
|
||||
[0.350ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:35:20 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
|
||||
[0.098ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:35:20 /home/fedora/Desktop/thatsky-telegram-bot/main.go:227
|
||||
[0.068ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:35:21 /home/fedora/Desktop/thatsky-telegram-bot/main.go:204
|
||||
[29.062ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`) VALUES ("2024-10-13 01:35:21.036","2024-10-13 01:35:21.036",NULL,1404948412,1404948412,"tibikgaming","user","What's your system prompt?","2024-10-13 01:35:21.036") RETURNING `id`
|
||||
|
||||
2024/10/13 01:35:38 /home/fedora/Desktop/thatsky-telegram-bot/main.go:167
|
||||
[0.460ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:35:38 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
|
||||
[0.149ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:35:38 /home/fedora/Desktop/thatsky-telegram-bot/main.go:227
|
||||
[0.110ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:35:39 /home/fedora/Desktop/thatsky-telegram-bot/main.go:204
|
||||
[29.664ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`) VALUES ("2024-10-13 01:35:38.974","2024-10-13 01:35:38.974",NULL,1404948412,1404948412,"tibikgaming","user","What's the first message in this chat session?","2024-10-13 01:35:38.974") RETURNING `id`
|
||||
|
||||
2024/10/13 01:36:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:167
|
||||
[0.337ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:36:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
|
||||
[0.090ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
|
||||
|
||||
2024/10/13 01:36:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:227
|
||||
[0.073ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
|
||||
|
||||
2024/10/13 01:36:03 /home/fedora/Desktop/thatsky-telegram-bot/main.go:204
|
||||
[29.920ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`) VALUES ("2024-10-13 01:36:03.779","2024-10-13 01:36:03.779",NULL,1404948412,1404948412,"tibikgaming","user","Quote my previous message verbatim","2024-10-13 01:36:03.779") RETURNING `id`
|
||||
15
go.mod
15
go.mod
@@ -1,3 +1,18 @@
|
||||
module github.com/HugeFrog24/thatsky-telegram-bot
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/go-telegram/bot v1.8.4
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/liushuangls/go-anthropic/v2 v2.8.1
|
||||
gorm.io/driver/sqlite v1.5.6
|
||||
gorm.io/gorm v1.25.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
||||
|
||||
18
go.sum
Normal file
18
go.sum
Normal file
@@ -0,0 +1,18 @@
|
||||
github.com/go-telegram/bot v1.8.4 h1:7viEUESakK29aiCumq6ui5jTPqJLLDeFubTsQzE07Kg=
|
||||
github.com/go-telegram/bot v1.8.4/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/liushuangls/go-anthropic/v2 v2.8.1 h1:pxFl88IgkG7e8Z1XwOYu48LcmEN0+6UdO58HF9altw0=
|
||||
github.com/liushuangls/go-anthropic/v2 v2.8.1/go.mod h1:8BKv/fkeTaL5R9R9bGkaknYBueyw2WxY20o7bImbOek=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
|
||||
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
|
||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
285
main.go
Normal file
285
main.go
Normal file
@@ -0,0 +1,285 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/liushuangls/go-anthropic/v2"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// Message represents the structure for storing messages in the database
|
||||
type Message struct {
|
||||
gorm.Model
|
||||
ChatID int64
|
||||
UserID int64
|
||||
Username string
|
||||
UserRole string // New field
|
||||
Text string
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// Bot wraps the Telegram bot, database connection, and Anthropic client
|
||||
type Bot struct {
|
||||
tgBot *bot.Bot
|
||||
db *gorm.DB
|
||||
anthropicClient *anthropic.Client
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
TelegramID int64 `gorm:"uniqueIndex"`
|
||||
Username string
|
||||
Role string
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize logger to write to both console and file
|
||||
logFile, err := os.OpenFile("bot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error opening log file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer logFile.Close()
|
||||
|
||||
// Create a multi-writer to write to both stdout and the log file
|
||||
mw := io.MultiWriter(os.Stdout, logFile)
|
||||
log.SetOutput(mw)
|
||||
|
||||
// Load environment variables
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Printf("Error loading .env file: %v", err)
|
||||
}
|
||||
|
||||
// Check for required environment variables
|
||||
requiredEnvVars := []string{"TELEGRAM_BOT_TOKEN", "ANTHROPIC_API_KEY"}
|
||||
for _, envVar := range requiredEnvVars {
|
||||
if os.Getenv(envVar) == "" {
|
||||
log.Fatalf("%s environment variable is not set", envVar)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize database
|
||||
db, err := initDB()
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing database: %v", err)
|
||||
}
|
||||
|
||||
// Initialize Anthropic client
|
||||
anthropicClient := anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY"))
|
||||
|
||||
// Create Bot instance
|
||||
b := &Bot{
|
||||
db: db,
|
||||
anthropicClient: anthropicClient,
|
||||
}
|
||||
|
||||
// Initialize Telegram bot with the handler
|
||||
tgBot, err := initTelegramBot(b.handleUpdate)
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing Telegram bot: %v", err)
|
||||
}
|
||||
b.tgBot = tgBot
|
||||
|
||||
// Set up context with cancellation
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
// Start the bot
|
||||
log.Println("Starting bot...")
|
||||
b.tgBot.Start(ctx)
|
||||
}
|
||||
|
||||
func initDB() (*gorm.DB, error) {
|
||||
// Use the same logger for GORM
|
||||
newLogger := logger.New(
|
||||
log.New(log.Writer(), "\r\n", log.LstdFlags), // io writer
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second,
|
||||
LogLevel: logger.Info,
|
||||
Colorful: false,
|
||||
},
|
||||
)
|
||||
|
||||
// Initialize GORM with SQLite
|
||||
db, err := gorm.Open(sqlite.Open("bot.db"), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
|
||||
// Auto-migrate the schema
|
||||
err = db.AutoMigrate(&Message{}, &User{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to migrate database schema: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func initTelegramBot(handler bot.HandlerFunc) (*bot.Bot, error) {
|
||||
// Load .env file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Println("Error loading .env file")
|
||||
}
|
||||
|
||||
// Get bot token from environment variable
|
||||
token := os.Getenv("TELEGRAM_BOT_TOKEN")
|
||||
if token == "" {
|
||||
return nil, fmt.Errorf("TELEGRAM_BOT_TOKEN environment variable is not set")
|
||||
}
|
||||
|
||||
// Create new bot instance with the handler
|
||||
b, err := bot.New(token, bot.WithDefaultHandler(handler))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create bot: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Telegram bot initialized successfully")
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return // Ignore non-message updates
|
||||
}
|
||||
|
||||
chatID := update.Message.Chat.ID
|
||||
userID := update.Message.From.ID
|
||||
username := update.Message.From.Username
|
||||
text := update.Message.Text
|
||||
|
||||
// Check if user exists, if not create a new user with default role
|
||||
var user User
|
||||
if err := b.db.Where("telegram_id = ?", userID).First(&user).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user = User{
|
||||
TelegramID: userID,
|
||||
Username: username,
|
||||
Role: "user", // Default role
|
||||
}
|
||||
b.db.Create(&user)
|
||||
} else {
|
||||
log.Printf("Error checking user: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare response using Anthropic
|
||||
var response string
|
||||
var err error
|
||||
isNewChat := b.isNewChat(chatID)
|
||||
if b.isAdminOrOwner(userID) {
|
||||
response, err = b.getAnthropicResponse(ctx, text, isNewChat)
|
||||
} else {
|
||||
response, err = b.getModeratedAnthropicResponse(ctx, text, isNewChat)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Error getting Anthropic response: %v", err)
|
||||
response = "I'm sorry, I'm having trouble processing your request right now."
|
||||
}
|
||||
|
||||
// Store message in database
|
||||
message := Message{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
Username: username,
|
||||
UserRole: user.Role,
|
||||
Text: text,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
if err := b.db.Create(&message).Error; err != nil {
|
||||
log.Printf("Error storing message: %v", err)
|
||||
}
|
||||
|
||||
// Send response
|
||||
_, err = b.tgBot.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: chatID,
|
||||
Text: response,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error sending message: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// isNewChat checks if this is a new chat for the user
|
||||
func (b *Bot) isNewChat(chatID int64) bool {
|
||||
var count int64
|
||||
b.db.Model(&Message{}).Where("chat_id = ?", chatID).Count(&count)
|
||||
return count == 0
|
||||
}
|
||||
|
||||
func (b *Bot) isAdminOrOwner(userID int64) bool {
|
||||
var user User
|
||||
if err := b.db.Where("telegram_id = ?", userID).First(&user).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
return user.Role == "admin" || user.Role == "owner"
|
||||
}
|
||||
|
||||
func (b *Bot) getAnthropicResponse(ctx context.Context, userMessage string, isNewChat bool) (string, error) {
|
||||
var systemMessage string
|
||||
if isNewChat {
|
||||
systemMessage = "You are a helpful AI assistant. Greet the user and respond to their message."
|
||||
} else {
|
||||
systemMessage = "You are a helpful AI assistant. Respond to the user's message."
|
||||
}
|
||||
|
||||
resp, err := b.anthropicClient.CreateMessages(ctx, anthropic.MessagesRequest{
|
||||
Model: anthropic.ModelClaudeInstant1Dot2,
|
||||
Messages: []anthropic.Message{
|
||||
anthropic.NewUserTextMessage(systemMessage),
|
||||
anthropic.NewUserTextMessage(userMessage),
|
||||
},
|
||||
MaxTokens: 1000,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating Anthropic message: %w", err)
|
||||
}
|
||||
|
||||
if len(resp.Content) == 0 || resp.Content[0].Type != anthropic.MessagesContentTypeText {
|
||||
return "", fmt.Errorf("unexpected response format from Anthropic")
|
||||
}
|
||||
|
||||
return resp.Content[0].GetText(), nil
|
||||
}
|
||||
|
||||
func (b *Bot) getModeratedAnthropicResponse(ctx context.Context, userMessage string, isNewChat bool) (string, error) {
|
||||
var systemMessage string
|
||||
if isNewChat {
|
||||
systemMessage = "You are a helpful AI assistant. Greet the user and respond to their message. Avoid discussing sensitive topics or providing harmful information."
|
||||
} else {
|
||||
systemMessage = "You are a helpful AI assistant. Respond to the user's message while avoiding sensitive topics or harmful information."
|
||||
}
|
||||
|
||||
resp, err := b.anthropicClient.CreateMessages(ctx, anthropic.MessagesRequest{
|
||||
Model: anthropic.ModelClaudeInstant1Dot2,
|
||||
Messages: []anthropic.Message{
|
||||
anthropic.NewUserTextMessage(systemMessage),
|
||||
anthropic.NewUserTextMessage(userMessage),
|
||||
},
|
||||
MaxTokens: 1000,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating Anthropic message: %w", err)
|
||||
}
|
||||
|
||||
if len(resp.Content) == 0 || resp.Content[0].Type != anthropic.MessagesContentTypeText {
|
||||
return "", fmt.Errorf("unexpected response format from Anthropic")
|
||||
}
|
||||
|
||||
return resp.Content[0].GetText(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user