1
+ import { makeTgBotClient } from "@effect-ak/tg-bot-client" ;
1
2
import type { GlobalState } from "#/tg-bot-playground/main" ;
2
3
import type { TsTextModel } from "#/common/editor/ts-text-model" ;
3
4
@@ -14,8 +15,6 @@ export const makeRunnableBot =
14
15
return ;
15
16
}
16
17
17
- console . log ( "code" , code . serialized )
18
-
19
18
worker . postMessage ( {
20
19
command : 'run-bot' ,
21
20
token : state . bot . token ,
@@ -28,7 +27,7 @@ export const makeRunnableBot =
28
27
} )
29
28
30
29
export const checkTokenAndRun =
31
- ( state : GlobalState , runnableBot : RunnableBot ) => {
30
+ async ( state : GlobalState , runnableBot : RunnableBot ) => {
32
31
33
32
const token = state . bot . token ;
34
33
@@ -37,21 +36,29 @@ export const checkTokenAndRun =
37
36
return
38
37
} ;
39
38
40
- fetch ( `https://api.telegram.org/bot${ token } /getMe` )
41
- . then ( _ => _ . json ( ) )
39
+ const client =
40
+ makeTgBotClient ( {
41
+ bot_token : token
42
+ } ) ;
43
+
44
+ await client . execute ( "get_me" , { } )
42
45
. then ( info => {
43
- if ( info . ok ) {
44
- state . bot . name = info . result . first_name ;
45
- state . bot . isReachable = true ;
46
- console . log ( "Running bot" )
47
- runnableBot ( state ) ;
48
- } else {
49
- state . bot . name = "nameless" ;
50
- state . bot . isReachable = false ;
51
- }
46
+ state . bot . name = info . first_name ;
47
+ state . bot . isReachable = true ;
48
+ console . log ( "Running bot" )
49
+ return runnableBot ( state ) ;
52
50
} ) . catch ( error => {
53
- console . warn ( "check token error" , error ) ;
51
+ state . botUpdates . push ( error ) ;
52
+ state . bot . name = "nameless" ;
54
53
state . bot . isReachable = false ;
55
54
} ) ;
56
55
56
+ const webhook =
57
+ await client . execute ( "get_webhook_info" , { } ) ;
58
+
59
+ if ( webhook . url ) {
60
+ state . botUpdates . push ( "Cannot work with webhooks, delete it first" ) ;
61
+ state . bot . isReachable = false ;
62
+ }
63
+
57
64
}
0 commit comments