From 9f2b3df4c86e267691664df84c21b8a52d077de3 Mon Sep 17 00:00:00 2001 From: HugeFrog24 <62775760+HugeFrog24@users.noreply.github.com> Date: Sun, 13 Oct 2024 02:58:18 +0200 Subject: [PATCH] Concern separation --- anthropic.go | 51 +++ bot.db | Bin 28672 -> 53248 bytes bot.go | 162 ++++++++++ bot.log | 824 +++++++++++++++++++++++++++++++++++++++--------- config.go | 26 ++ config.json | 6 + database.go | 52 +++ go.mod | 1 + go.sum | 2 + handlers.go | 72 +++++ main.go | 258 ++------------- models.go | 36 +++ rate_limiter.go | 48 +++ 13 files changed, 1147 insertions(+), 391 deletions(-) create mode 100644 anthropic.go create mode 100644 bot.go create mode 100644 config.go create mode 100644 config.json create mode 100644 database.go create mode 100644 handlers.go create mode 100644 models.go create mode 100644 rate_limiter.go diff --git a/anthropic.go b/anthropic.go new file mode 100644 index 0000000..0417140 --- /dev/null +++ b/anthropic.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "fmt" + + "github.com/liushuangls/go-anthropic/v2" +) + +func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Message, isNewChat, isAdminOrOwner bool) (string, error) { + var systemMessage string + if isNewChat { + systemMessage = "You are a helpful AI assistant." + } else { + systemMessage = "Continue the conversation." + } + + if !isAdminOrOwner { + systemMessage += " Avoid discussing sensitive topics or providing harmful information." + } + + // Ensure the roles are correct + for i := range messages { + if messages[i].Role == "user" { + messages[i].Role = anthropic.RoleUser + } else if messages[i].Role == "assistant" { + messages[i].Role = anthropic.RoleAssistant + } + } + + model := anthropic.ModelClaude3Dot5Sonnet20240620 + if !isAdminOrOwner { + model = anthropic.ModelClaudeInstant1Dot2 + } + + resp, err := b.anthropicClient.CreateMessages(ctx, anthropic.MessagesRequest{ + Model: model, + Messages: messages, + System: systemMessage, + 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 +} diff --git a/bot.db b/bot.db index 6661ef228e09c51353f329d2c407363d13cae669..f0d3c3c4c6f5e75c1bfe45cbffeb358e58852ecb 100644 GIT binary patch literal 53248 zcmeHQO>i8?b>78-_#?m(MZwSyX*Nm8AS7@#|38o`HpD;{9EuW2C=4r+iNS6!c8uAX z)yynGICi-fQd057m*k*J4oa1yiz-#Q#D^qRNu``a4)Mhol}owAl}c4AIk*zXRd%KF zUe6DJogSK!BPx;`B{dg=*WItb_tWpa?%lm{?TVLB(g~xUn-Hz`P_16CeUXq_t!Bdi zbMSxuGX*ck^IzcK`k~iVFU?x>&bM%o>6stYPW;P>KRWT`+&AYIXaDZ_pB{f69*G}; zfIvVXAP^7;2m}NI0s(=*FO7gdJ-J{l)Q1aR(5832_TAfqm`3sKg!*(la(lPEHhVC0 z`TE-G#u|C`>iV@;*U0+SUtjwzvdN|2Brjhjo2=L7#mz$lZ7yw1O)eNP+p&?^+Av!} z+qd0>b(_v6E6s91cXX0N&zJTepPXD+Sg5~wDsi`b$_8Z3R5t3y>ho9Dat6soSi~mr zf`o3EkFSyQ z!%q+KbBOjZ6#0xW0^l9EJ-SH}dN*0#j6$CR>IF~BmtVemW8?bj`qd4x+1bhDgH)U3 zg_p0dt$*n%WA|dwbBSDEdtvSR+SSWzHwb$ggCJnYrKPQjQ32pU@$CbK+3%BO0gTe6 z0$@*J@Pq!FOTT$+Vqs~a{=0>Y)2_Hi_G%&enKSh_>uk(?-u&OOBcp8Q zj~^)Bt*)CKF%f1&SXl=!7u;n;$d;P;k?8VI1Hc2o5&GlMK$H3c; zNk2iKJ16~2`pN9O$KOA`KJ(p~N2lYdA5OkAIW_UzvF{w48vjb;=Ga^HyS4XzY4`kK z1{j{6pRL`vQ5zq>Fn{UBv?{CmB}KlZXhc?5G=0U;o4R5csw_LsGw?{3?}OyVUw{9v z@c+cy@^-e}o)>JRt6L4)WQ#%#g^~os_=Sp*bj!ANBb{vj%p-G*y{WTj_n*BnRqCQy zO;fdO*>nuMg1<5e^0pes9>_gN-2BBxmy$a)+9Mq=iWBhCaS{z$i5CX&8aVc1g4#!R z-I(l!0}{J?urPA|Me6(E`6jtZ7w=Hgb=$-xTaiaQ=;))cosAKDV3#lq4ajbn0tPrn zPC?uFYZOzzLtyv~1g=@JSWA;lOG_6!{QRjxd_9i%G_z?dx}{jUs#n2NBB>DHOTs&Dw@p4IutcPMLhNG%l2Hc2h z@JN1v6j9xdf-owuVo*b|w9L4Mk3W=h3ikL6M_#(#R20?GG)3p@o3>IW;mhk*I9LQL z*`T{qZ?Ib5*;G#e}~ z7w(r&9O(Eo2Qy7=>Z)crma1i4N^32X@G*PdjX`6w4 zA@lKWM4Mza9(6Cv&<(ee59?TPQ#vf-xJh10kwS^4UaRbUven6&QHXT@cp=iW9IH{) zrfoWo1z|vydX`BlMH&*fN7l=-%!W`kS&on&&v8RAA?KY+08LMXlHx$|QVI zUiENi4oQ#Fgo%vVL)p05c%a<2QMjDb(*<}udw?EbJ+LO(ya$sbBo*Ktk}eCZN?w9Z z27bgjkgYT|Tf26xHV*FkF&>6;Q&vn(ceE;Rt&CFe5o9qC2Qb!Kh4>?n&W0x^3&0jQ z+9o?q-7pQ+bYy-!r>&GpDga|ll~78%>ke!iSwuU$GEsPq;oK-ps*PUFupI^5hRT&9 zmxK=#dW|@YqCGHwcnQG>#*h#u4xnIx(Fo*j5Gii6aF9fp&Ot01dE4EDfKwSU=wf17 zw`DswoRi0@Ax?Y1c4KI=oD1bBMqHBnhq&mjJG$Z&o8b6du816n%@xIljl(u{%i@Y% zxQkw}z(cjfFb9Qqq`#H9ajoTa$h+{kQaE>EES)mHt`!C+R)u2h#VY ze~`W>{f+dO(qBk_F1;(gBmIf=$I@HUA4=brz9qdWeN+0HG?4nzj?|G{>35`?(sk)2 z=|$dPeIZa|}JWA_+RwjEuDH!t(v;FS$|l*DbryUkyzvmxipT$)V>-e#g`dj4LH`yj!) z0C@i2kbYE?{zLka^wUqVfW^`T0s;YnfIvVXAP^7;2m}NI0s(=5KtLeyDMw%$Zu_%} zL<3*3Lc-W&W1hViAkF`OoZbKblmj7_ClC+_2m}NI0s(=5KtLcM5D*9m1Ox&CfnON} z%J={4(%UuoPy7f31Ofs9fq+0jARrJB2nYlO0s;YnfIvVX@NpsVT78OrPuzu(udvHL z&0o<>`0@k(w`Xxl@Vf%ox2&XJUx(-ab?N7D{{L|;P|Pb35C{ka1Ofs9fq+0jARrJB z2nYlO0s;Ynz(Eq}8J?b>Wfv#LFU()MF>-lA(^m|=sVjz|%Ch6|K7^4=f{2Y=pui7N zTn*Y}i*~|@lBDY1KpA+Y-eM{UeLmY(}3)DDPVwOjpb9hbuOKW`o7$ z!u|4z10A2{V5X@}UDYhdQnjp0X{}`vK4#CmF{mgwEEH}l48WbmF7se#1QIu_iN4vRQ$l9y7XP@<{VDm$NSb+Tp@BAq{8i1aMS zYE-ppn~q~a7*M62Ws*vfhQ#fW^|CCpAyiFPGg%JL&lIlaIgTh5*!dk>)@4KCempcd z#WD$Blvh36nM2Z}G+`oR_E0u%HXbOqZ4@r&^mG9p&mN!$SP!g8Ht)gY2uTHahosAH zrj{&=O$L6%IgqV1HCwxOtu_ws`Y|4ca#L1JO?R{^Z>@|{@DXG&5C<^UTZQ-|kIsfC zCkwz9INByVP2DgJ)pTTjJg2RcNh$ziOqEbdyXy{Y8(Bm z+=j}PBA0{@6nc#~jG{d-e|QPO2*!{QCJwfIiqS|5;s<=h_UDMcK7bg4MBa8cA>dR- z47!+D)@|9&4d>*sYKYSwu-zD%EayTwiV>IO{vj^9>yEBC#U?mDmn$L%Vsk~YVdJn3 z-Lkl1$2;1i6b}U8d|utuo%}Q7hbJ5N0GRbKRZ~+fN8{#9>0(AmJ^;Y^3^0MA=dsU~ zv3uz-bkUnCT!A-am2d0FREgw4LChw$U>mn`eVME05YLs|M0R0c*Ay_}+!K%JFsuf) z`lBQqh&e9eV06{cb9=}9f42Vh+KG2hj8FgP^ybvxPdzd5vxyfP|I_fs9)UlIpN|ZI z;rzK=7v@U=if-ZeN~n&ZD2{A!cQGyrM;EX}aT8)TPJmJjA%lN1^I-EjDNOE-((bC8 zE4tCNEfv1MqGH?Nl2n_Vdo zTBhP~HwrFEg|Jg#jyjHm{+RS+In0}DpDiT)``;W55TJ`{G$BGZ4FeipnSx950VNIc z^+Svm=-q@#Jfa;aBSBH=AiKbvBCT@736|NQD&a$L;RbOl^7_y%?37Z&CMmK;7IX9? zddCX~F=3$xRKK8{QDluwqp$#ktYBo)1NB2euVd{irU9gvEYoDEO+@=pG+_Y_){gG8 za8yxXON%O4IE0$OJu7YZSzaTr_!RPG;zQ8iqiN3Oby)l#P)MWEqFTNic^$8nCedM( zRgcv8GD7OmL6B}v)u<>N(P*Tn^NriIn=B3J9 zT1;_pb%@<=g+3^&I$eCC5b7F7sIYeUx*-#?8m&sGWs(Y^rUsSf28EqwCddOF zCWfk^mX}+H3y-I)LOS?V$&DE+27Wz~WkK%6|HdVqN5YpR3#^E{?RvqeW#of^yM$6q z2I9%b3J^>2UOFvUk!?a6%?FVUIzmzbA}8i6W`I1^&aL$i<{m8+|J%|iT$-_>16+_s zL297zi%C3^D!9N~L)pe9y@8*2m(Y`^NB zk9!fs3&~cpx|YBe{fhUIe2-;D-aEqJ;z`gqnzKbm3Se z1~%af+1do+35Y#_S%wEcNWAx(KLzLUAAubub8lt1txhPY`;j zxq^NaH4&UP(~#KhP{4%g99swCS0Kvthp8g2eT%w-#LLWPBs9&98}bU&DGk^`35Bq3maJ>l1hqTO#56tqtZyu#6pqM}qzQpcmo|I4Ff0d3H?7^aevX z=zfawF#P({2ZuSYj|Lb96h1YGgmf7~A?|S*kAfdwaL(9^oj<$4^P!8KMgDZSMmZ7- zM5cD06+y1{@lsj07VnFFVuhJ|QLMv5iu!qugGzCuTO`BU_ z;F44b?K&%a-cOaL7}Zi$UCAbX<4d0_%=hx)+JSLqOt$SQh%;URgla2CVYI2xvy zP0NHjpb6DO?s$oWDXZyFqN$TnWWwq(byWUNfW7YEuwe*YvpDk^TR6})Tr5& ZyaV5bTF6{dvRAg`6igE?9_MGh{|hq-k|Y2C literal 28672 zcmeI3OK;p%6vyq%WSn`$MWP*b5phw4Xw+oz|g;%Y}g>AvS0xV*mTnk8#aK{1^0S+WMQT?;)4x=c0{uekwom96XO?bYjO<=W21+B%85vbw&rCPBC%utq0zJQ`@AZ5aMgHw*$)$EOPkIa^uXmX0 zYK*=;9CVYA#eP9*TwD-G&rGnh!KSjyRu%IJW}8ypW9)20Ce2r8N{tH_#OpEk@Mf1> z!?r{J=8@mu^D~Qu6G_NyJWq$cL?Rc-CvPcE<;8za<>*pTYBU<+-9hXd7gTI!PxO@( z=#$KLx;j~XjWeFkZz^$hGHzK~7UY}r_vhcKe_wyO_F3)u>PMBoDmThMl~?9Ik$xeY zVFCg`00;m9An^YsFj|(Y!uGaMES}rkKCLMlURIT5RY!{2(#@8lH!aJt73?_H1x0Hq z%44`}e|_&O`ig?x;Ksh!3;O$;M+5SeeD4lA9QwC{!BL2kvmkWKAMSeO9B@jzmZIvG zJ?@&GkBpporZ>pxHCnpebS!LXii0s%FVBUe_g!*|`C^Eo1M)jUFbt!K+@McZ1|g#7 zpeQ6K$HAcgYDPQj!_*W@OKX3j&a|&^v}NI&sS|n~tNHI(cba#yl6U&H?gzXe}#?g&P=~hLMtW zs>)!aE%Q*$=MMm1X+Zkh^K9YbgPwUbS9T{voZ75yky$Bx2MB^4{e#s)n+ zWmBaZ^yuUmmZ2HRPBtSZ@4%NbIGhrQO_P-K_`@=)6d0J(SuiB6PucfUi$qlQ~EEY@;D<~fgt}*3`{@(2mk>f z00e*l5C8%|00;m9AOHk_z<-*++)SZSj_*;ZiG>A`-Bux2$f#ubb3uM6|3SV3_)UHw z-f00e*l5C8&CoItT4h%zPIcg+oO4Q6!Q>3Oq%}k*nlZ87`gspn%SEp$`hAo-?Gk1JK|9Mfp#G z{KEtUfB+Bx0zd!=00AHX1b_e#00KY&2s~v3q(ZSwzYZ8*|34)6|375|gy8@IAOHk_ a01yBIKmZ5;0U!VbfB+Bx0x chatMemory.Size { + chatMemory.Messages = chatMemory.Messages[2:] + } +} + +func (b *Bot) prepareContextMessages(chatMemory *ChatMemory) []anthropic.Message { + b.chatMemoriesMu.RLock() + defer b.chatMemoriesMu.RUnlock() + + var contextMessages []anthropic.Message + for _, msg := range chatMemory.Messages { + role := anthropic.RoleUser + if !msg.IsUser { + role = anthropic.RoleAssistant + } + contextMessages = append(contextMessages, anthropic.Message{ + Role: role, + Content: []anthropic.MessageContent{ + anthropic.NewTextMessageContent(msg.Text), + }, + }) + } + return contextMessages +} + +func (b *Bot) isNewChat(chatID int64) bool { + var count int64 + b.db.Model(&Message{}).Where("chat_id = ?", chatID).Count(&count) + return count == 1 +} + +func (b *Bot) isAdminOrOwner(userID int64) bool { + var user User + err := b.db.Preload("Role").Where("telegram_id = ?", userID).First(&user).Error + if err != nil { + return false + } + return user.Role.Name == "admin" || user.Role.Name == "owner" +} + +func initTelegramBot(handleUpdate func(ctx context.Context, b *bot.Bot, update *models.Update)) (*bot.Bot, error) { + opts := []bot.Option{ + bot.WithDefaultHandler(handleUpdate), + } + + return bot.New(os.Getenv("TELEGRAM_BOT_TOKEN"), opts...) +} diff --git a/bot.log b/bot.log index cdb6731..65dc62f 100644 --- a/bot.log +++ b/bot.log @@ -1,228 +1,740 @@ -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 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.028ms] [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 +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[28.510ms] [rows:0] CREATE TABLE `messages` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`chat_id` integer,`user_id` integer,`username` text,`user_role` text,`text` text,`timestamp` datetime,`is_user` numeric) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.213ms] [rows:0] CREATE INDEX `idx_messages_deleted_at` ON `messages`(`deleted_at`) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.064ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles" + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.650ms] [rows:0] CREATE TABLE `roles` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.510ms] [rows:0] CREATE UNIQUE INDEX `idx_roles_name` ON `roles`(`name`) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.811ms] [rows:0] CREATE INDEX `idx_roles_deleted_at` ON `roles`(`deleted_at`) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.086ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users" + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.542ms] [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_id` integer,CONSTRAINT `fk_users_role` FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`)) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.475ms] [rows:0] CREATE INDEX `idx_users_deleted_at` ON `users`(`deleted_at`) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[19.446ms] [rows:0] CREATE UNIQUE INDEX `idx_users_telegram_id` ON `users`(`telegram_id`) + +2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.155ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[19.868ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:06.988","2024-10-13 02:26:06.988",NULL,"user") RETURNING `id` + +2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.144ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[19.695ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:07.008","2024-10-13 02:26:07.008",NULL,"admin") RETURNING `id` + +2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.135ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[19.908ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:07.028","2024-10-13 02:26:07.028",NULL,"owner") RETURNING `id` +2024/10/13 02:26:07 Telegram bot initialized successfully +2024/10/13 02:26:07 Starting bot... + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 record not found +[0.300ms] [rows:0] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:198 +[0.182ms] [rows:1] SELECT * FROM `roles` WHERE name = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:203 +[29.608ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`telegram_id`,`username`,`role_id`) VALUES ("2024-10-13 02:26:13.713","2024-10-13 02:26:13.713",NULL,1404948412,"tibikgaming",1) RETURNING `id` + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[20.376ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:13.743","2024-10-13 02:26:13.743",NULL,1404948412,1404948412,"tibikgaming","","Hello","2024-10-13 02:26:13.743",true) RETURNING `id` + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.243ms] [rows:1] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20 + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.061ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.051ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.218ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 +2024/10/13 02:26:14 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant' + +2024/10/13 02:26:14 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.404ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:14.218","2024-10-13 02:26:14.218",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:26:14.218",false) RETURNING `id` + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.120ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.641ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.264ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:18.626","2024-10-13 02:26:18.626",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:26:18.626",true) RETURNING `id` + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.220ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.109ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.376ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 +2024/10/13 02:26:18 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant' + +2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.141ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:18.888","2024-10-13 02:26:18.888",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:26:18.888",false) RETURNING `id` + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.010ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.024ms] [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 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.008ms] [rows:-] SELECT * FROM `messages` LIMIT 1 + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.006ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.031ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.005ms] [rows:-] SELECT * FROM `roles` LIMIT 1 + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.017ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.005ms] [rows:-] SELECT * FROM `users` LIMIT 1 + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.018ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %") + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id" + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.040ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.014ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.015ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 +2024/10/13 02:28:24 Telegram bot initialized successfully +2024/10/13 02:28:24 Starting bot... + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.579ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.086ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:28:31.237","2024-10-13 02:28:31.237",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:28:31.237",true) RETURNING `id` + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.352ms] [rows:5] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20 + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.081ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.080ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.248ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 +2024/10/13 02:28:31 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant' + +2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.128ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:28:31.589","2024-10-13 02:28:31.589",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:28:31.589",false) RETURNING `id` + +2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.022ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages" + +2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 [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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.021ms] [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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.020ms] [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 /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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.017ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.066ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" 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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.017ms] [rows:-] SELECT * FROM `roles` 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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.019ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.028ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.030ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" 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.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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.011ms] [rows:-] SELECT * FROM `users` LIMIT 1 -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %") -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143 +[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id" -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.055ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.019ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 -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 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152 +[0.023ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 +2024/10/13 02:33:02 Telegram bot initialized successfully +2024/10/13 02:33:02 Starting bot... -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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.091ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.479ms] [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: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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[30.934ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:09.456","2024-10-13 02:33:09.456",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:33:09.456",true) RETURNING `id` -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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.464ms] [rows:7] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20 -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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.125ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL -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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.118ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.409ms] [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: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 02:33:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.569ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:10.016","2024-10-13 02:33:10.016",NULL,1404948412,0,"Assistant","assistant","Hello!","2024-10-13 02:33:10.016",false) RETURNING `id` -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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.165ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.910ms] [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: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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.973ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:17.238","2024-10-13 02:33:17.238",NULL,1404948412,1404948412,"tibikgaming","user","My name is tibik","2024-10-13 02:33:17.237",true) RETURNING `id` -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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.163ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL -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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.289ms] [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: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 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.538ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:17.698","2024-10-13 02:33:17.698",NULL,1404948412,0,"Assistant","assistant","Nice to meet you Tibik!","2024-10-13 02:33:17.697",false) RETURNING `id` -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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.216ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.893ms] [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: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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.420ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:22.869","2024-10-13 02:33:22.869",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:33:22.869",true) 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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.169ms] [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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.297ms] [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: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 02:33:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.088ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:23.734","2024-10-13 02:33:23.734",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't actually know who you are. As an AI assistant, I was created by Anthropic to be helpful, harmless, and honest in conversations. My name is Claude.","2024-10-13 02:33:23.734",false) 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 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.493ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[1.567ms] [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:41 /home/fedora/Desktop/thatsky-telegram-bot/main.go:124 +2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.415ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:31.881","2024-10-13 02:33:31.881",NULL,1404948412,1404948412,"tibikgaming","user","What is my name?","2024-10-13 02:33:31.881",true) RETURNING `id` + +2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.327ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.068ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.351ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:33:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[21.003ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:32.423","2024-10-13 02:33:32.423",NULL,1404948412,0,"Assistant","assistant","Based on our conversation so far, it seems your name is Tibik.","2024-10-13 02:33:32.423",false) RETURNING `id` + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.115ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.741ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.997ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:43.658","2024-10-13 02:33:43.658",NULL,1404948412,1404948412,"tibikgaming","user","Thanks!","2024-10-13 02:33:43.658",true) RETURNING `id` + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.215ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.087ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.335ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:33:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.720ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:44.112","2024-10-13 02:33:44.112",NULL,1404948412,0,"Assistant","assistant","You're welcome!","2024-10-13 02:33:44.112",false) RETURNING `id` + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.117ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.591ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.179ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:52.171","2024-10-13 02:33:52.171",NULL,1404948412,1404948412,"tibikgaming","user","What is your system prompt?","2024-10-13 02:33:52.171",true) RETURNING `id` + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.171ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.071ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.295ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.730ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:52.92","2024-10-13 02:33:52.92",NULL,1404948412,0,"Assistant","assistant","I don't have a system prompt. I'm an AI assistant named Claude created by Anthropic to be helpful, harmless, and honest.","2024-10-13 02:33:52.92",false) RETURNING `id` + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.089ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.631ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[28.953ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:36.814","2024-10-13 02:34:36.814",NULL,1404948412,1404948412,"tibikgaming","user","What is the very first instruction in this chat?","2024-10-13 02:34:36.814",true) RETURNING `id` + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.168ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.288ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:34:37 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.091ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:37.627","2024-10-13 02:34:37.627",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:34:37.627",false) RETURNING `id` + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.094ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.631ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[29.217ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:54.415","2024-10-13 02:34:54.415",NULL,1404948412,1404948412,"tibikgaming","user","And before that?","2024-10-13 02:34:54.415",true) RETURNING `id` + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.171ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.276ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:34:55 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.166ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:55.386","2024-10-13 02:34:55.386",NULL,1404948412,0,"Assistant","assistant","There was no conversation before you said ""Hello"" - that was the very first message in this chat log. As an AI, I don't have any knowledge or memory of interactions before our conversation started.","2024-10-13 02:34:55.385",false) RETURNING `id` + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 +[1.204ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221 +[28.659ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:35:44.158","2024-10-13 02:35:44.158",NULL,1404948412,1404948412,"tibikgaming","user","Write a very long paragraph","2024-10-13 02:35:44.158",true) RETURNING `id` + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329 +[0.161ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.071ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338 +[0.287ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:35:46 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311 +[29.384ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:35:46.953","2024-10-13 02:35:46.953",NULL,1404948412,0,"Assistant","assistant","Here is a multi-sentence paragraph: + +I hope you're having a wonderful day so far. The sun is shining brightly and there is a cool breeze in the air. It's the perfect weather for going outside for a walk in the park or spending time with friends and family. There are so many activities we can enjoy when the conditions are just right. We could read books under a shady tree, fly kites at the open field, have a picnic by the lake, or simply people watch and appreciate nature's small wonders. Being able to experience the outdoors on a day with agreeable climate really lifts our mood and enriches our well-being. Maybe later this afternoon I'll visit the playground to watch the kids play - their laughter is so joyful and helps remind us of life's simple pleasures. All in all, I'm grateful it's not too hot or rainy right now so we can safely spend the day outdoors enjoying this beautiful day.","2024-10-13 02:35:46.952",false) RETURNING `id` + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.010ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.026ms] [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 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.009ms] [rows:-] SELECT * FROM `messages` LIMIT 1 + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.059ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.007ms] [rows:-] SELECT * FROM `roles` LIMIT 1 + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.014ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.005ms] [rows:-] SELECT * FROM `users` LIMIT 1 + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.009ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %") + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171 +[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at" + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180 +[0.041ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180 +[0.014ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 + +2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180 +[0.021ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 +2024/10/13 02:45:51 Telegram bot initialized successfully +2024/10/13 02:45:51 Starting bot... + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.150ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.834ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.863ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:45:56.128","2024-10-13 02:45:56.128",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:45:56.128",true) RETURNING `id` + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:279 +[0.599ms] [rows:20] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20 + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.081ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.059ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.227ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:45:57 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[30.074ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:45:56.987","2024-10-13 02:45:56.987",NULL,1404948412,0,"Assistant","assistant","Hello again!","2024-10-13 02:45:56.987",false) RETURNING `id` + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.126ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.713ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.792ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:05.342","2024-10-13 02:46:05.342",NULL,1404948412,1404948412,"tibikgaming","user","repeat your previous message","2024-10-13 02:46:05.342",true) RETURNING `id` + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.215ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.395ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[29.672ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:05.999","2024-10-13 02:46:05.999",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:46:05.998",false) RETURNING `id` + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.861ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.179ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:24.184","2024-10-13 02:46:24.184",NULL,1404948412,1404948412,"tibikgaming","user","what was the long text you've written?","2024-10-13 02:46:24.184",true) RETURNING `id` + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.172ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.304ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:26 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[29.708ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:25.997","2024-10-13 02:46:25.997",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have any long texts to reference. As an AI, I don't store full transcripts of our conversation. Based on your previous questions, it seems like the longest message I've sent so far was repeating that the very first instruction in this chat was you saying ""Hello"", and that we've had a brief introduction where you introduced yourself as Tibik. Please let me know if you need any clarification or have additional questions!","2024-10-13 02:46:25.997",false) RETURNING `id` + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.086ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.494ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[28.367ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:45.045","2024-10-13 02:46:45.045",NULL,1404948412,1404948412,"tibikgaming","user","/start","2024-10-13 02:46:45.045",true) RETURNING `id` + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.151ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.106ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.351ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[29.792ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:45.872","2024-10-13 02:46:45.872",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have access to any system commands like ""/start"". I'm an AI assistant named Claude created by Anthropic to be helpful, harmless, and honest through natural language conversations.","2024-10-13 02:46:45.872",false) RETURNING `id` + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.128ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.805ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.541ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:07.295","2024-10-13 02:47:07.295",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:47:07.295",true) RETURNING `id` + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.186ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.062ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.301ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:08 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[29.566ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:08.415","2024-10-13 02:47:08.415",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I don't actually have any information about who you are. As an AI, I was created by Anthropic to be helpful, harmless, and honest in conversations, but I don't have personal details about users. You'd have to introduce yourself for me to know your name or identity.","2024-10-13 02:47:08.415",false) RETURNING `id` + +2024/10/13 02:47:15 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.158ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:15 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.766ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.550ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:15.998","2024-10-13 02:47:15.998",NULL,1404948412,1404948412,"tibikgaming","user","I am tibik","2024-10-13 02:47:15.998",true) RETURNING `id` + +2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.240ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.386ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[28.911ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:16.767","2024-10-13 02:47:16.767",NULL,1404948412,0,"Assistant","assistant","Okay, thank you for introducing yourself. Based on our conversation so far, it's nice to meet you Tibik!","2024-10-13 02:47:16.767",false) RETURNING `id` + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237 +[0.684ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263 +[29.679ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:22.839","2024-10-13 02:47:22.839",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:47:22.839",true) RETURNING `id` + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371 +[0.211ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380 +[0.479ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:47:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353 +[29.105ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:24.138","2024-10-13 02:47:24.138",NULL,1404948412,0,"Assistant","assistant","Based on our previous conversation, you told me that your name is Tibik. Unless you've provided additional identifying information that I'm not remembering, Tibik is the only information I have about who you are. Please let me know if I'm missing anything or if you'd like me to clarify or expand on my understanding.","2024-10-13 02:47:24.138",false) RETURNING `id` + +2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.030ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages" + +2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.084ms] [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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 [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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[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: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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.021ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.080ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" 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 -[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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.021ms] [rows:-] SELECT * FROM `roles` LIMIT 1 -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.023ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.021ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.018ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.063ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.021ms] [rows:-] SELECT * FROM `users` LIMIT 1 -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.058ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %") -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30 +[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id" -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47 +[0.098ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 -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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47 +[0.032ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`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 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47 +[0.038ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1 +2024/10/13 02:56:14 Starting bot... -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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55 +[0.118ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55 +[0.682ms] [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: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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86 +[29.400ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:31.931","2024-10-13 02:56:31.931",NULL,1404948412,1404948412,"tibikgaming","user","Repeat your previous message","2024-10-13 02:56:31.931",true) RETURNING `id` -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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:96 +[0.763ms] [rows:20] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20 -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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:143 +[0.125ms] [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:221 -[0.149ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL +2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149 +[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`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 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149 +[0.387ms] [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 02:56:33 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86 +[28.522ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:33.108","2024-10-13 02:56:33.108",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:56:33.108",false) 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 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55 +[0.124ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL -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 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55 +[0.741ms] [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: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 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86 +[28.963ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:42.374","2024-10-13 02:56:42.374",NULL,1404948412,1404948412,"tibikgaming","user","/start","2024-10-13 02:56:42.374",true) RETURNING `id` -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` +2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:143 +[0.169ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL + +2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149 +[0.111ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL + +2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149 +[0.387ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + +2024/10/13 02:56:43 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86 +[29.535ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:43.154","2024-10-13 02:56:43.154",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have any system commands like ""/start"". I'm an AI assistant named Claude having a conversation.","2024-10-13 02:56:43.153",false) RETURNING `id` diff --git a/config.go b/config.go new file mode 100644 index 0000000..9ec29d8 --- /dev/null +++ b/config.go @@ -0,0 +1,26 @@ +package main + +import ( + "encoding/json" + "os" +) + +type Config struct { + MemorySize int `json:"memory_size"` + MessagePerHour int `json:"messages_per_hour"` + MessagePerDay int `json:"messages_per_day"` + TempBanDuration string `json:"temp_ban_duration"` +} + +func loadConfig(filename string) (Config, error) { + var config Config + file, err := os.Open(filename) + if err != nil { + return config, err + } + defer file.Close() + + decoder := json.NewDecoder(file) + err = decoder.Decode(&config) + return config, err +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..25c405d --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "memory_size": 10, + "messages_per_hour": 20, + "messages_per_day": 100, + "temp_ban_duration": "24h" +} \ No newline at end of file diff --git a/database.go b/database.go new file mode 100644 index 0000000..bd0a9e3 --- /dev/null +++ b/database.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "log" + "time" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func initDB() (*gorm.DB, error) { + newLogger := logger.New( + log.New(log.Writer(), "\r\n", log.LstdFlags), + logger.Config{ + SlowThreshold: time.Second, + LogLevel: logger.Info, + Colorful: false, + }, + ) + + 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) + } + + err = db.AutoMigrate(&Message{}, &User{}, &Role{}) + if err != nil { + return nil, fmt.Errorf("failed to migrate database schema: %w", err) + } + + err = createDefaultRoles(db) + if err != nil { + return nil, err + } + + return db, nil +} + +func createDefaultRoles(db *gorm.DB) error { + roles := []string{"user", "admin", "owner"} + for _, roleName := range roles { + var role Role + if err := db.FirstOrCreate(&role, Role{Name: roleName}).Error; err != nil { + return fmt.Errorf("failed to create default role %s: %w", roleName, err) + } + } + return nil +} diff --git a/go.mod b/go.mod index 7ad491b..c485318 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ 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 + golang.org/x/time v0.7.0 gorm.io/driver/sqlite v1.5.6 gorm.io/gorm v1.25.12 ) diff --git a/go.sum b/go.sum index b431973..bb232af 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o 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= +golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= +golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= 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= diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..95c9252 --- /dev/null +++ b/handlers.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "log" + + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" +) + +func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.Update) { + if update.Message == nil { + return + } + + chatID := update.Message.Chat.ID + userID := update.Message.From.ID + + if !b.checkRateLimits(userID) { + b.sendRateLimitExceededMessage(ctx, chatID) + return + } + + username := update.Message.From.Username + text := update.Message.Text + + user, err := b.getOrCreateUser(userID, username) + if err != nil { + log.Printf("Error getting or creating user: %v", err) + return + } + + userMessage := b.createMessage(chatID, userID, username, user.Role.Name, text, true) + b.storeMessage(userMessage) + + chatMemory := b.getOrCreateChatMemory(chatID) + b.addMessageToChatMemory(chatMemory, userMessage) + + contextMessages := b.prepareContextMessages(chatMemory) + + response, err := b.getAnthropicResponse(ctx, contextMessages, b.isNewChat(chatID), b.isAdminOrOwner(userID)) + if err != nil { + log.Printf("Error getting Anthropic response: %v", err) + response = "I'm sorry, I'm having trouble processing your request right now." + } + + b.sendResponse(ctx, chatID, response) + + assistantMessage := b.createMessage(chatID, 0, "Assistant", "assistant", response, false) + b.storeMessage(assistantMessage) + b.addMessageToChatMemory(chatMemory, assistantMessage) +} + +func (b *Bot) sendRateLimitExceededMessage(ctx context.Context, chatID int64) { + _, err := b.tgBot.SendMessage(ctx, &bot.SendMessageParams{ + ChatID: chatID, + Text: "Rate limit exceeded. Please try again later.", + }) + if err != nil { + log.Printf("Error sending rate limit message: %v", err) + } +} + +func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string) { + _, err := b.tgBot.SendMessage(ctx, &bot.SendMessageParams{ + ChatID: chatID, + Text: text, + }) + if err != nil { + log.Printf("Error sending message: %v", err) + } +} diff --git a/main.go b/main.go index 88f57a2..034fe4c 100644 --- a/main.go +++ b/main.go @@ -2,73 +2,29 @@ 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) + // Initialize logger + logFile, err := initLogger() if err != nil { - fmt.Fprintf(os.Stderr, "Error opening log file: %v\n", err) - os.Exit(1) + log.Fatalf("Error initializing logger: %v", err) } 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) - } - } + checkRequiredEnvVars() // Initialize database db, err := initDB() @@ -76,21 +32,17 @@ func main() { log.Fatalf("Error initializing database: %v", err) } - // Initialize Anthropic client - anthropicClient := anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY")) + // Load configuration + config, err := loadConfig("config.json") + if err != nil { + log.Fatalf("Error loading configuration: %v", err) + } // Create Bot instance - b := &Bot{ - db: db, - anthropicClient: anthropicClient, - } - - // Initialize Telegram bot with the handler - tgBot, err := initTelegramBot(b.handleUpdate) + b, err := NewBot(db, config) if err != nil { - log.Fatalf("Error initializing Telegram bot: %v", err) + log.Fatalf("Error creating bot: %v", err) } - b.tgBot = tgBot // Set up context with cancellation ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) @@ -98,188 +50,24 @@ func main() { // Start the bot log.Println("Starting bot...") - b.tgBot.Start(ctx) + b.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, - }) +func initLogger() (*os.File, error) { + logFile, err := os.OpenFile("bot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { - return nil, fmt.Errorf("failed to connect to database: %w", err) + return nil, 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 + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) + return logFile, 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 +func checkRequiredEnvVars() { + 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) } } - - // 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 } diff --git a/models.go b/models.go new file mode 100644 index 0000000..7b9e415 --- /dev/null +++ b/models.go @@ -0,0 +1,36 @@ +package main + +import ( + "time" + + "gorm.io/gorm" +) + +type Message struct { + gorm.Model + ChatID int64 + UserID int64 + Username string + UserRole string + Text string + Timestamp time.Time + IsUser bool +} + +type ChatMemory struct { + Messages []Message + Size int +} + +type Role struct { + gorm.Model + Name string `gorm:"uniqueIndex"` +} + +type User struct { + gorm.Model + TelegramID int64 `gorm:"uniqueIndex"` + Username string + RoleID uint + Role Role `gorm:"foreignKey:RoleID"` +} diff --git a/rate_limiter.go b/rate_limiter.go new file mode 100644 index 0000000..b0a5111 --- /dev/null +++ b/rate_limiter.go @@ -0,0 +1,48 @@ +package main + +import ( + "time" + + "golang.org/x/time/rate" +) + +type userLimiter struct { + hourlyLimiter *rate.Limiter + dailyLimiter *rate.Limiter + lastReset time.Time + banUntil time.Time +} + +func (b *Bot) checkRateLimits(userID int64) bool { + b.userLimitersMu.Lock() + defer b.userLimitersMu.Unlock() + + limiter, exists := b.userLimiters[userID] + if !exists { + limiter = &userLimiter{ + hourlyLimiter: rate.NewLimiter(rate.Every(time.Hour/time.Duration(b.config.MessagePerHour)), b.config.MessagePerHour), + dailyLimiter: rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay), + lastReset: time.Now(), + } + b.userLimiters[userID] = limiter + } + + now := time.Now() + + if now.Before(limiter.banUntil) { + return false + } + + if now.Sub(limiter.lastReset) >= 24*time.Hour { + limiter.dailyLimiter = rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay) + limiter.lastReset = now + } + + if !limiter.hourlyLimiter.Allow() || !limiter.dailyLimiter.Allow() { + banDuration, _ := time.ParseDuration(b.config.TempBanDuration) + limiter.banUntil = now.Add(banDuration) + return false + } + + return true +}