1+ import { repository } from '@roll-stack/database'
12import { Bot } from 'grammy'
23
34const logger = useLogger ( 'telegram:wasabi-vista' )
@@ -12,27 +13,50 @@ export async function useCreateWasabiVistaBot() {
1213 // const locale = ctx.message.from.language_code
1314
1415 if ( ctx . hasCommand ( 'start' ) ) {
15- // Welcome message with buttons
16- await ctx . reply (
17- `Ключ доступа: ${ generateAccessCode ( ) } ` ,
18- // {
19- // reply_markup: {
20- // inline_keyboard: [
21- // [{ text: dictionary(locale).bots.woodland.title, url: woodlandsBotUrl }],
22- // [{ text: dictionary(locale).bots.subscribeToChannel, url: gameChannelUrl }],
23- // [{ text: dictionary(locale).bots.chatgame.playingOnTwitch, url: twitchUrl }],
24- // ],
25- // },
26- // },
27- )
16+ // Not private chat?
17+ if ( ctx . message . chat . type !== 'private' ) {
18+ await ctx . reply ( 'Я пока не умею отвечать на групповые сообщения.' )
19+ return
20+ }
2821
22+ // Find user
23+ const wasabiVistaUser = await repository . wasabiVista . findUserByTelegramId ( ctx . message . from . id . toString ( ) )
24+ if ( ! wasabiVistaUser ) {
25+ const accessKey = await generateAccessCode ( )
26+
27+ const createdUser = await repository . wasabiVista . createUser ( {
28+ telegramId : ctx . message . from . id . toString ( ) ,
29+ accessKey,
30+ firstName : ctx . message . from . first_name ,
31+ lastName : ctx . message . from . last_name ,
32+ username : ctx . message . from . username ,
33+ } )
34+
35+ logger . log ( 'new user' , createdUser . id , ctx . message . from . id , ctx . message . text )
36+
37+ await ctx . reply ( `Ключ доступа: ${ accessKey } ` )
38+
39+ return
40+ }
41+
42+ if ( ! wasabiVistaUser . user ) {
43+ await ctx . reply ( 'Нет доступа. Используйте ранее полученный Ключ доступа. Или передайте его в службу поддержки.' )
44+ return
45+ }
46+
47+ await ctx . reply ( 'Вы уже авторизованы.' )
2948 return
3049 }
3150
3251 logger . log ( ctx . message . from . id , ctx . message . text )
3352 ctx . reply ( 'Я пока не умею отвечать на сообщения.' )
3453 } )
3554
55+ // Somebody invited bot to a group
56+ bot . on ( 'my_chat_member' , async ( ctx ) => {
57+ logger . log ( ctx . chat , ctx . chatMember , ctx . message )
58+ } )
59+
3660 try {
3761 await bot . start ( )
3862 logger . info ( 'Wasabi Vista bot started successfully' )
@@ -54,6 +78,17 @@ export async function notifyWasabiVistaAdmin(message: string) {
5478 return useWasabiVistaBot ( ) . api . sendMessage ( telegram . adminId , message )
5579}
5680
57- function generateAccessCode ( ) : string {
58- return getRandInteger ( 100000 , 999999 ) . toString ( )
81+ async function generateAccessCode ( ) : Promise < string > {
82+ let selectedCode
83+
84+ // Code should be unique
85+ while ( ! selectedCode ) {
86+ const code = getRandInteger ( 100000 , 999999 ) . toString ( )
87+ const wasabiVistaUser = await repository . wasabiVista . findUserByKey ( code )
88+ if ( ! wasabiVistaUser ) {
89+ selectedCode = code
90+ }
91+ }
92+
93+ return selectedCode
5994}
0 commit comments