@@ -12,9 +12,7 @@ import {
1212const token = Deno . env . get ( "TG_BOT_TOKEN" ) ;
1313const MAIN_CHAT_ID = parseInt ( Deno . env . get ( "TG_MAIN_CHAT_ID" ) ! ) ;
1414const DOMAIN = Deno . env . get ( "DOMAIN" ) ;
15- const STICEKR_SET_NAME = Deno . env . get ( "STICKER_SET_NAME" ) ! ;
16- const TOM_SLAMA_STICEKR_SET = Deno . env . get ( "TOM_SLAMA_STICKER_SET" ) ! ;
17- const MARIAN_STICEKR_SET = Deno . env . get ( "MARIAN_STICKER_SET" ) ! ;
15+ const STICEKR_SET_NAME = Deno . env . get ( "STICKER_SET_NAME" ) ;
1816const STICEKR_SET_OWNER = parseInt ( Deno . env . get ( "STICKER_SET_OWNER" ) ! ) ;
1917
2018export const webhookPath = "/tg-webhook" ;
@@ -34,65 +32,6 @@ const webhookUrlToken = genRandomToken(96);
3432let lastRequestTime = 0 ;
3533const MIN_REQUEST_INTERVAL = 100 ; // Minimum 100ms between requests
3634
37- function getAvailableSticekrSets ( ) : string [ ] {
38- const sets = [ ] ;
39- if ( STICEKR_SET_NAME ) sets . push ( STICEKR_SET_NAME ) ;
40- if ( TOM_SLAMA_STICEKR_SET ) sets . push ( TOM_SLAMA_STICEKR_SET ) ;
41- if ( MARIAN_STICEKR_SET ) sets . push ( MARIAN_STICEKR_SET ) ;
42- return sets ;
43- }
44-
45- function editDistance ( a : string , b : string ) : number {
46- const matrix = Array ( a . length + 1 ) . fill ( null ) . map ( ( ) =>
47- Array ( b . length + 1 ) . fill ( 0 )
48- ) ;
49-
50- for ( let i = 0 ; i <= a . length ; i ++ ) matrix [ i ] [ 0 ] = i ;
51- for ( let j = 0 ; j <= b . length ; j ++ ) matrix [ 0 ] [ j ] = j ;
52-
53- for ( let i = 1 ; i <= a . length ; i ++ ) {
54- for ( let j = 1 ; j <= b . length ; j ++ ) {
55- if ( a [ i - 1 ] === b [ j - 1 ] ) {
56- matrix [ i ] [ j ] = matrix [ i - 1 ] [ j - 1 ] ;
57- } else {
58- matrix [ i ] [ j ] = Math . min (
59- matrix [ i - 1 ] [ j ] + 1 ,
60- matrix [ i ] [ j - 1 ] + 1 ,
61- matrix [ i - 1 ] [ j - 1 ] + 1 ,
62- ) ;
63- }
64- }
65- }
66-
67- return matrix [ a . length ] [ b . length ] ;
68- }
69-
70- function getSticekrCommand ( text : string ) : string | null {
71- const normalizedText = text . toLowerCase ( ) . trim ( ) ;
72-
73- const commands = [ "sticker this" , "marian this" , "tom this" ] ;
74-
75- for ( const command of commands ) {
76- if ( editDistance ( normalizedText , command ) <= 2 ) {
77- return command ;
78- }
79- }
80-
81- return null ;
82- }
83-
84- function getSticekrSetForCommand ( command : string ) : string {
85- switch ( command ) {
86- case "marian this" :
87- return MARIAN_STICEKR_SET ;
88- case "tom this" :
89- return TOM_SLAMA_STICEKR_SET ;
90- case "sticker this" :
91- default :
92- return STICEKR_SET_NAME ;
93- }
94- }
95-
9635async function rateLimitedDelay ( ) {
9736 const now = Date . now ( ) ;
9837 const timeSinceLastRequest = now - lastRequestTime ;
@@ -200,31 +139,20 @@ async function domeny() {
200139 }
201140
202141 if ( Math . random ( ) < 0.5 ) {
203- const availableSets = getAvailableSticekrSets ( ) ;
204- const allSticekrs = [ ] ;
205- for ( const setName of availableSets ) {
206- try {
207- const {
208- result : { stickers : sticekrs } ,
209- } = await tgCall (
210- {
211- name : setName ,
212- } ,
213- "getStickerSet" ,
214- ) ;
215- allSticekrs . push ( ...sticekrs ) ;
216- } catch ( error ) {
217- console . log ( `Failed to get stickers from set ${ setName } :` , error ) ;
218- }
219- }
220-
221- if ( allSticekrs . length === 0 ) return ;
222-
223- const randomSticekr = allSticekrs [ Math . floor ( Math . random ( ) * allSticekrs . length ) ] ;
142+ const {
143+ result : { stickers : sticekrs } ,
144+ } = await tgCall (
145+ {
146+ name : STICEKR_SET_NAME ,
147+ } ,
148+ "getStickerSet" ,
149+ ) ;
150+ const { file_id : sticekr } =
151+ sticekrs [ Math . floor ( Math . random ( ) * sticekrs . length ) ] ;
224152 await tgCall (
225153 {
226154 chat_id : MAIN_CHAT_ID ,
227- sticker : randomSticekr . file_id ,
155+ sticker : sticekr ,
228156 } ,
229157 "sendSticker" ,
230158 ) ;
@@ -320,15 +248,13 @@ export async function init() {
320248 ! DOMAIN ||
321249 isNaN ( MAIN_CHAT_ID ) ||
322250 ! STICEKR_SET_NAME ||
323- ! TOM_SLAMA_STICEKR_SET ||
324- ! MARIAN_STICEKR_SET ||
325251 isNaN ( STICEKR_SET_OWNER )
326252 ) {
327253 console . log (
328- `TG_BOT_TOKEN: ${ token } , TG_MAIN_CHAT_ID: ${ MAIN_CHAT_ID } , DOMAIN: ${ DOMAIN } , STICEKR_SET_NAME: ${ STICEKR_SET_NAME } , TOM_SLAMA_STICEKR_SET: ${ TOM_SLAMA_STICEKR_SET } , MARIAN_STICEKR_SET: ${ MARIAN_STICEKR_SET } , STICEKR_SET_OWNER: ${ STICEKR_SET_OWNER } ` ,
254+ `TG_BOT_TOKEN: ${ token } , TG_MAIN_CHAT_ID: ${ MAIN_CHAT_ID } , DOMAIN: ${ DOMAIN } , STICEKR_SET_NAME: ${ STICEKR_SET_NAME } , STICEKR_SET_OWNER: ${ STICEKR_SET_OWNER } ` ,
329255 ) ;
330256 throw new Error (
331- "TG_BOT_TOKEN, TG_MAIN_CHAT_ID, DOMAIN, STICEKR_SET_NAME, TOM_SLAMA_STICEKR_SET, MARIAN_STICEKR_SET or STICEKR_SET_OWNER is not set" ,
257+ "TG_BOT_TOKEN, TG_MAIN_CHAT_ID, DOMAIN, STICEKR_SET_NAME or STICEKR_SET_OWNER is not set" ,
332258 ) ;
333259 }
334260
@@ -582,12 +508,11 @@ Be grateful for your abilities and your incredible success and your considerable
582508 yield * handleLogo ( data , text . slice ( 6 ) ) ;
583509 }
584510
585- const sticekrCommand = getSticekrCommand ( text ) ;
586511 if (
587- sticekrCommand &&
512+ text . toLowerCase ( ) === "sticker this" &&
588513 data . message . chat . id === MAIN_CHAT_ID
589514 ) {
590- const result = yield * sticekrThis ( data . message . reply_to_message , sticekrCommand ) ;
515+ const result = yield * sticekrThis ( data . message . reply_to_message ) ;
591516 if ( result !== null ) {
592517 yield await tgCall ( {
593518 chat_id : data . message . chat . id ,
@@ -961,7 +886,7 @@ async function* handleInlineQuery(data: any) {
961886 }
962887}
963888
964- async function * sticekrThis ( orig_msg : any , command : string ) : AsyncGenerator < any , string | null > {
889+ async function * sticekrThis ( orig_msg : any ) : Promise < string | null > {
965890 if ( ! orig_msg ) return "wtf" ;
966891 let file ;
967892 if ( Array . isArray ( orig_msg . photo ) ) {
@@ -995,11 +920,9 @@ async function* sticekrThis(orig_msg: any, command: string): AsyncGenerator<any,
995920 if ( res . code !== 0 ) return "imagemagick is a hoe" ;
996921 const sticekr = await Deno . readFile ( outFileName ) ;
997922
998- const targetSticekrSet = getSticekrSetForCommand ( command ) ;
999-
1000923 const body = new FormData ( ) ;
1001924 body . append ( "user_id" , STICEKR_SET_OWNER . toString ( ) ) ;
1002- body . append ( "name" , targetSticekrSet ) ;
925+ body . append ( "name" , STICEKR_SET_NAME ) ;
1003926 body . append (
1004927 "sticker" ,
1005928 JSON . stringify ( { sticker : "attach://file" , emoji_list : [ "🤓" ] } ) ,
@@ -1016,12 +939,12 @@ async function* sticekrThis(orig_msg: any, command: string): AsyncGenerator<any,
1016939
1017940 const data4 = await tgCall (
1018941 {
1019- name : targetSticekrSet ,
942+ name : STICEKR_SET_NAME ,
1020943 } ,
1021944 "getStickerSet" ,
1022945 ) ;
1023946 if ( ! data4 . ok ) {
1024- return "i ran out of error message ideas: " + JSON . stringify ( data4 ) ;
947+ return "i ran out of error message ideas: " + JSON . stringify ( resp4 ) ;
1025948 }
1026949 const sticekrId = data4 . result . stickers . at ( - 1 ) . file_id ;
1027950 if ( ! sticekrId ) return "i ran out of error message ideas the most" ;
0 commit comments