Skip to content

Commit 08e36af

Browse files
committed
fix: queue bug
1 parent 5293ff4 commit 08e36af

File tree

5 files changed

+89
-50
lines changed

5 files changed

+89
-50
lines changed

server/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const app = express();
1313
app.use(express.json());
1414

1515
initRoutes()
16-
// telegramBot.start()
17-
app.use(`/api/bot/${TELEGRAM_BOT_TOKEN}`, webhookCallback(telegramBot, "express"));
16+
telegramBot.start()
17+
// app.use(`/api/bot/${TELEGRAM_BOT_TOKEN}`, webhookCallback(telegramBot, "express"));
1818
app.use("/api", (req:Request, res:Response) => {
1919
res.json('Welcome to Fingu!')
2020
});

server/src/telegramRoutes/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { telegramBot } from "../utils/telegram"
44
import { telegramFileHandler } from "./telegramFileHandler"
55
import { telegramMessageHandler } from "./telegramMessageHandler"
66
import { telegramCommandHandler } from "./telegramCommandHandler"
7-
import { chatAction } from "../telegramMiddlewares/chatAction"
87
import { limit } from "@grammyjs/ratelimiter"
8+
import { telegramQueueEndHandler, telegramQueueStartHandler } from "./telegramQueueHandler"
99

1010
telegramBot.use(limit({
1111
limit: 100,
@@ -15,13 +15,17 @@ telegramBot.use(limit({
1515
}
1616
}))
1717

18+
telegramBot.use(telegramQueueStartHandler)
19+
1820
telegramBot.use(logger)
1921

2022
telegramBot.on("msg::bot_command", telegramCommandHandler)
2123

2224
telegramBot.on("message:file", telegramFileHandler)
2325

24-
telegramBot.on("message", telegramMessageHandler)
26+
telegramBot.on("message:text", telegramMessageHandler)
27+
28+
telegramBot.use(telegramQueueEndHandler)
2529

2630
telegramBot.use((ctx: Context, next: NextFunction) => {
2731

server/src/telegramRoutes/telegramCommandHandler/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ export const telegramCommandHandler = async (ctx: Context, next: NextFunction) =
2929
console.log(err)
3030

3131
}
32+
finally {
33+
34+
next()
35+
36+
}
3237

3338
}

server/src/telegramRoutes/telegramMessageHandler/index.ts

+5-25
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ export const telegramMessageHandler = async (ctx: Context, next: NextFunction) =
1616

1717
const fromName = ctx.message.from.first_name ?? "anonymous"
1818

19-
// block new message
20-
const setIsWaiting = await prisma.telegram.upsert({
21-
where: {
22-
id: fromTelegramId,
23-
},
24-
update: {
25-
id: fromTelegramId,
26-
isWaiting: true
27-
},
28-
create: {
29-
id: fromTelegramId,
30-
isWaiting: true
31-
}
32-
})
33-
3419
const newMessage = {
3520
isUser: true,
3621
text: ctx.message.text ?? "..."
@@ -122,22 +107,17 @@ export const telegramMessageHandler = async (ctx: Context, next: NextFunction) =
122107
]
123108
})
124109

125-
// update isWaiting
126-
const setIsWaiting = await prisma.telegram.update({
127-
where: {
128-
id: fromTelegramId
129-
},
130-
data: {
131-
isWaiting: false
132-
}
133-
})
134-
135110
console.timeEnd('telegramMessageHandler');
136111

137112
} catch (err) {
138113

139114
console.log(err)
140115

141116
}
117+
finally{
118+
119+
next()
120+
121+
}
142122

143123
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,89 @@
11
import { Context, NextFunction } from "grammy";
22
import { prisma } from "../../utils/prisma";
33

4-
export const telegramQueueHandler = async (ctx:Context,next:NextFunction) => {
4+
export const telegramQueueStartHandler = async (ctx: Context, next: NextFunction) => {
55
if (!ctx.message) return
66

77
const fromTelegramId = ctx.message?.from.id.toString()
8-
8+
ctx.replyWithChatAction('typing')
99
// check for isWaiting
10-
let checkIsWaiting = await prisma.telegram.findFirst({
11-
where:{
12-
id:fromTelegramId
13-
},
14-
select:{
15-
isWaiting:true,
10+
try {
11+
let checkIsWaiting = await prisma.telegram.findFirst({
12+
where: {
13+
id: fromTelegramId
14+
},
15+
select: {
16+
isWaiting: true,
17+
}
18+
})
19+
20+
// if it's first time user, id would be false
21+
if (!checkIsWaiting) {
22+
next()
23+
}
24+
25+
// short polling the db
26+
// inneficient, will be updated to long polling or pubsub when scaling is needed
27+
while (checkIsWaiting?.isWaiting) {
28+
console.log("🚀 ~ file: index.ts:28 ~ telegramQueueHandler ~ checkIsWaiting:", checkIsWaiting)
29+
await new Promise(r => setTimeout(r, 10000))
30+
checkIsWaiting = await prisma.telegram.findFirst({
31+
where: {
32+
id: fromTelegramId
33+
},
34+
select: {
35+
isWaiting: true,
36+
}
37+
})
1638
}
17-
})
1839

19-
// if it's first time user, id would be false
20-
if (!checkIsWaiting) {
40+
await prisma.telegram.upsert({
41+
where: {
42+
id: fromTelegramId
43+
},
44+
update: {
45+
isWaiting: true
46+
},
47+
create: {
48+
isWaiting: true,
49+
id: fromTelegramId
50+
}
51+
})
52+
53+
54+
}
55+
catch (err) {
56+
57+
console.log(err)
58+
59+
}
60+
finally{
61+
2162
next()
63+
2264
}
65+
}
66+
2367

24-
// short polling the db
25-
// inneficient, will be updated to long polling or pubsub when scaling is needed
26-
while (checkIsWaiting?.isWaiting) {
27-
await new Promise(r => setTimeout(r, 10000))
28-
checkIsWaiting = await prisma.telegram.findFirst({
29-
where:{
30-
id:fromTelegramId
68+
export const telegramQueueEndHandler = async (ctx: Context, next: NextFunction) => {
69+
if (!ctx.message) return
70+
71+
const fromTelegramId = ctx.message?.from.id.toString()
72+
73+
try {
74+
75+
await prisma.telegram.update({
76+
where: {
77+
id: fromTelegramId
3178
},
32-
select:{
33-
isWaiting:true,
79+
data: {
80+
isWaiting: false
3481
}
3582
})
83+
3684
}
85+
catch (err) {
3786

38-
next()
87+
console.log(err)
88+
}
3989
}

0 commit comments

Comments
 (0)