@@ -63,31 +63,41 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
6363 const jobName = job == null ? jobKey : job . name ;
6464
6565 const throttledCall = getThrottled ( chatId , async function ( endpoint , body ) {
66- await fetch ( `https://api.telegram.org/bot${ token } /${ endpoint } ` , {
66+ const res = await fetch ( `https://api.telegram.org/bot${ token } /${ endpoint } ` , {
6767 method : 'post' ,
6868 body : JSON . stringify ( body ) ,
6969 headers : { 'Content-Type' : 'application/json' } ,
7070 } ) ;
71+ return res ;
7172 } ) ;
7273
7374 const promises = newListings . map ( async ( o ) => {
7475 const img = normalizeImageUrl ( o . image ) ;
76+ const textPayload = {
77+ chat_id : chatId ,
78+ text : buildText ( jobName , serviceName , o ) ,
79+ parse_mode : 'HTML' ,
80+ disable_web_page_preview : true ,
81+ } ;
82+
83+ if ( ! img ) {
84+ return throttledCall ( 'sendMessage' , textPayload ) ;
85+ }
7586
76- if ( img ) {
77- return throttledCall ( 'sendPhoto' , {
87+ try {
88+ return await throttledCall ( 'sendPhoto' , {
7889 chat_id : chatId ,
7990 photo : img ,
8091 caption : buildCaption ( jobName , serviceName , o ) ,
8192 parse_mode : 'HTML' ,
8293 } ) ;
94+ } catch ( e ) {
95+ // If we see a timeout due to sending an image, try sending it without
96+ if ( e && ( e . code === 'ETIMEDOUT' || e . errno === 'ETIMEDOUT' ) ) {
97+ return throttledCall ( 'sendMessage' , textPayload ) ;
98+ }
99+ throw e ;
83100 }
84-
85- return throttledCall ( 'sendMessage' , {
86- chat_id : chatId ,
87- text : buildText ( jobName , serviceName , o ) ,
88- parse_mode : 'HTML' ,
89- disable_web_page_preview : true ,
90- } ) ;
91101 } ) ;
92102
93103 return Promise . all ( promises ) ;
0 commit comments