Rocketchat -> telegram


#1

Hey Guys, I am the idiot who wrote the terrible integration for telegram, i am obviously not a programmer but somehow managed to get this working. I am here to ask the people who actually do know what they are doing to update the telegram integration, to have more features.

class Script {
    process_incoming_request({ request }) {
        // UNCOMMENT THE BELOW LINE TO DEBUG IF NEEDED.
        console.log(request.content);
        if ('edited_message' in request.content) {
            request.content.message = request.content.edited_message;
        }
        let who = request.content.message.from.username
        let icon_url = '/avatar/' + request.content.message.from.username + '.jpg'
        if(!who)  {
          who = `${request.content.message.from.first_name} ${request.content.message.from.last_name}`
          icon_url = `/avatar/${request.content.message.from.first_name}.jpg`
        }
        let body = request.content.message.text
        //let photo = request.content.message.attachments.image_url

        //if(!body) {
        //  if(request.content.message.sticker.emoji) {
        //    // It's a sticker
        //    body = request.content.message.sticker.emoji
        //} else {
        //   return {}
        //  }
        //}

        if(request.content.message.sticker) {
          if(request.content.message.sticker.emoji) {
            // It's a sticker
            body = request.content.message.sticker.emoji
        } else {
           return {}
          }
        }
        if(request.content.message.document) {
          if(request.content.message.document) {
            // It's a sticker
            body = 'I send a gif :guenna:' 
        } else {
           return {}
          }
        }
        if(request.content.message.photo) {
          console.log(request.content.message.photo);
          if(request.content.message.photo[0]) {
            // It's a sticker
            //// 'https://api.telegram.org/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/getFile?file_id=${request.content.message.photo[0].file_path}'
            //body = 'i uploaded a pic ' + ' ![photo]('+`https://api.telegram.org/file/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/${request.content.message.photo[0].file_path}`+')'
            body = 'i uploaded a pic'
            //body = 'https://api.telegram.org/file/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/'+`${request.content.message.photo[0].file_path}`
            //request.content.message.attachments.image_url = `https://api.telegram.org/file/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/${request.content.message.photo[0].file_path}`
        } else {
           return {}
          }
        }
        if(request.content.message.video) {
          if(request.content.message.video.file_id) {
            // It's a sticker
            // works body = `https://api.telegram.org/file/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/${request.content.message.photo[0].file_path}`
            body = 'video upload'
            //request.content.message.attachments.image_url = `https://api.telegram.org/file/bot434433230:AAFc4-HZQJDApkMIS3KZR4ankoPYhbLx8bw/` + ${request.content.message.photo[0].file_path}
        } else {
           return {}
          }
        }
        return {
            content: {
                username: who,
                icon_url: icon_url,
                text: body
            }
        };
    }
}

Right now with every media that someone sends in the telegram channel you’ll receive simple replacements such as this maybe someone can write a better version. Cheers
image