File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed
5
5
import { onMessage } from "./server-bot/on-message.ts" ;
6
6
import { onCallbackQuery } from "./server-bot/on-callback-query.ts" ;
7
7
import { onStart } from "./server-bot/on-start.ts" ;
8
+ import { ignoreOldMessageMiddleware } from "./server-bot/ignore-old-messages-middleware.ts" ;
8
9
9
10
export const onRequestPost : PagesFunction = handleError (
10
11
async ( { env, request } ) => {
@@ -15,6 +16,7 @@ export const onRequestPost: PagesFunction = handleError(
15
16
}
16
17
17
18
const bot = new Bot ( envSafe . BOT_TOKEN ) ;
19
+ bot . use ( ignoreOldMessageMiddleware ) ;
18
20
bot . command ( "start" , onStart ) ;
19
21
bot . on ( "message" , onMessage ( envSafe ) ) ;
20
22
bot . on ( "callback_query:data" , onCallbackQuery ( envSafe ) ) ;
Original file line number Diff line number Diff line change
1
+ import { Context , NextFunction } from "grammy" ;
2
+
3
+ const threshold = 5 * 60 ; // 5 minutes
4
+ export async function ignoreOldMessageMiddleware (
5
+ ctx : Context ,
6
+ next : NextFunction
7
+ ) : Promise < void > {
8
+ if ( ctx . message ) {
9
+ if ( new Date ( ) . getTime ( ) / 1000 - ctx . message . date < threshold ) {
10
+ await next ( ) ;
11
+ } else {
12
+ console . log (
13
+ `Ignoring message from ${ ctx . from ?. id ?? 'Unknown ID' } at ${
14
+ ctx . chat ?. id ?? 'Unknown Chat ID'
15
+ } (${ new Date ( ) . getTime ( ) / 1000 } :${ ctx . message . date } )`
16
+ ) ;
17
+ }
18
+ } else {
19
+ await next ( ) ;
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments