1+ // deno-lint-ignore-file no-explicit-any
12// The authors disclaim copyright to this source code (they are ashamed to
23// admit they wrote it)
34
@@ -14,9 +15,15 @@ const MAIN_CHAT_ID = parseInt(Deno.env.get("TG_MAIN_CHAT_ID")!);
1415const DOMAIN = Deno . env . get ( "DOMAIN" ) ! ;
1516const STICEKR_SET_NAME = Deno . env . get ( "STICKER_SET_NAME" ) ! ;
1617const STICEKR_SET_OWNER = parseInt ( Deno . env . get ( "STICKER_SET_OWNER" ) ! ) ;
18+ const PRINTER_TOKEN = Deno . env . get ( "PRINTER_TOKEN" ) ! ;
1719
1820export const webhookPath = "/tg-webhook" ;
1921
22+ export type RequestEvent = {
23+ request : Request ;
24+ respondWith ( r : Response ) : Promise < void > ;
25+ } ;
26+
2027function genRandomToken ( bytes : number ) {
2128 return btoa (
2229 String . fromCharCode ( ...crypto . getRandomValues ( new Uint8Array ( bytes ) ) )
@@ -57,7 +64,7 @@ async function tgCall(
5764 // Apply rate limiting before making the request
5865 await rateLimitedDelay ( ) ;
5966
60- let req = await fetch ( `https://api.telegram.org/bot${ token } /${ endpoint } ` , {
67+ const req = await fetch ( `https://api.telegram.org/bot${ token } /${ endpoint } ` , {
6168 method : "POST" ,
6269 headers : {
6370 "Content-Type" : "application/json" ,
@@ -66,7 +73,7 @@ async function tgCall(
6673 } ) ;
6774
6875 try {
69- let resp = await req . json ( ) ;
76+ const resp = await req . json ( ) ;
7077
7178 // Handle rate limiting with exponential backoff
7279 if ( ! resp . ok && resp . error_code === 429 && retryCount < maxRetries ) {
@@ -91,7 +98,7 @@ async function tgCall(
9198 console . log ( "Req to" , endpoint , "with" , options , "failed:" , resp ) ;
9299 }
93100 return resp ;
94- } catch ( e ) {
101+ } catch {
95102 // Handle network errors with exponential backoff
96103 if ( retryCount < maxRetries ) {
97104 const delay = baseDelay * Math . pow ( 2 , retryCount ) ;
@@ -294,7 +301,7 @@ export async function init() {
294301 postGeohash ( ) ;
295302}
296303
297- export async function handleRequest ( e : Deno . RequestEvent ) {
304+ export async function handleRequest ( e : RequestEvent ) {
298305 if (
299306 e . request . method . toUpperCase ( ) !== "POST" ||
300307 e . request . headers . get ( "X-Telegram-Bot-Api-Secret-Token" ) !== webhookUrlToken
@@ -330,7 +337,7 @@ async function processTgUpdate(data: any) {
330337 for await ( const data of handleTgUpdate ( dato ) ) {
331338 for await ( const dato of handleTgUpdate ( data ) ) {
332339 for await ( const data of handleTgUpdate ( dato ) ) {
333- for await ( const dato of handleTgUpdate ( data ) ) {
340+ for await ( const _ of handleTgUpdate ( data ) ) {
334341 tgCall ( { text : "🔥" } ) ;
335342 }
336343 }
@@ -529,6 +536,29 @@ Be grateful for your abilities and your incredible success and your considerable
529536 yield * handleLogo ( data , text . slice ( 6 ) ) ;
530537 }
531538
539+ const trig = "/řekni_tomovi" ;
540+ if ( text . startsWith ( trig ) && data . message . chat . id === MAIN_CHAT_ID ) {
541+ const response = await fetch ( "https://printomat.slama.dev/submit" , {
542+ headers : {
543+ "Content-Type" : "application/x-www-form-urlencoded" ,
544+ } ,
545+ body : new URLSearchParams ( {
546+ message : `${ data . message . from } říká: ${ text . slice ( trig . length ) . trim ( ) } ` ,
547+ image : "" ,
548+ token : PRINTER_TOKEN ,
549+ } ) . toString ( ) ,
550+ method : "POST" ,
551+ } ) ;
552+ const txt = await response . text ( ) ;
553+ await tgCall ( {
554+ chat_id : data . message . chat . id ,
555+ reply_to_message_id : data . message . message_id ,
556+ text : `Tom říká (${ response . status } ): ${
557+ / < p > ( .* ?) < \/ p > / . exec ( txt ) ?. [ 1 ] ?? txt
558+ } `,
559+ } ) ;
560+ }
561+
532562 if (
533563 text . toLowerCase ( ) === "sticker this" &&
534564 data . message . chat . id === MAIN_CHAT_ID
@@ -750,17 +780,16 @@ async function generateLogos(text: string, filename: string) {
750780 const texted = LOGO_TEMPLATE . replace ( "TEMPLATETEXT" , text . trim ( ) ) ;
751781 await Deno . writeTextFile ( `./static/persistent/logos/${ filename } .svg` , texted ) ;
752782 return (
753- await Deno . run ( {
754- cmd : [
755- "inkscape" ,
783+ await new Deno . Command ( "inkscape" , {
784+ args : [
756785 `./static/persistent/logos/${ filename } .svg` ,
757786 "-o" ,
758787 `./static/persistent/logos/${ filename } .png` ,
759788 "-w" ,
760789 LOGO_RENDER_SIZE . toString ( ) ,
761790 ] ,
762791 stderr : "null" ,
763- } ) . status ( )
792+ } ) . spawn ( ) . status
764793 ) . code ;
765794}
766795
@@ -879,13 +908,13 @@ async function* reportProcessResult(
879908 exitCode : number
880909) {
881910 const outPath = `${ tempDir } /${ id } .out` ;
882- const fileProc = Deno . run ( {
883- cmd : [ "file" , "-ib" , outPath ] ,
911+ const fileProc = new Deno . Command ( "file" , {
912+ args : [ "-ib" , outPath ] ,
884913 stdout : "piped" ,
885- } ) ;
914+ } ) . spawn ( ) ;
915+ const out = await fileProc . output ( ) ;
886916 // need to await the status to not create zombie processes
887- await fileProc . status ( ) ;
888- const mime = decoder . decode ( await fileProc . output ( ) ) ;
917+ const mime = decoder . decode ( out . stdout ) ;
889918 contentTypes . set ( id , mime ) ;
890919 const isText =
891920 mime . startsWith ( "text/" ) || mime . startsWith ( "application/json" ) ;
@@ -920,17 +949,15 @@ async function handleCallbackQuery(data: any) {
920949 if ( cbData . startsWith ( "kill:" ) ) {
921950 const proc = runningProcesses . get ( cbData . slice ( 5 ) ) ;
922951 if ( proc === undefined ) return ;
923- const killProc = Deno . run ( {
924- cmd : [ "rkill" , "-9" , proc . pid . toString ( ) ] ,
925- } ) ;
926- await killProc . status ( ) ;
952+ const killProc = new Deno . Command ( "rkill" , {
953+ args : [ "-9" , proc . pid . toString ( ) ] ,
954+ } ) . spawn ( ) ;
955+ await killProc . status ;
927956 return ;
928957 }
929958}
930959
931- export async function handleTgWeb (
932- e : Deno . RequestEvent
933- ) : Promise < Response | null > {
960+ export async function handleTgWeb ( e : RequestEvent ) : Promise < Response | null > {
934961 const url = new URL ( e . request . url ) ;
935962 const path = url . pathname . slice ( 7 ) ;
936963 const ct = contentTypes . get ( path ) ;
0 commit comments