RocketChat / RocketChat/Rocket.Chat

Simple Telegram Bot/SlackBridge Not working - Errors out.

Open
#29,660 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
46.1k
Forks
13.9k
Avg merge
3d 3h
Merged PRs (30d)
130

Description

This code for Telegram seems to just error out in rocket Chat (latest). Seperately SlackBridge does not seem to work either.
(https://docs.rocket.chat/use-rocket.chat/workspace-administration/integrations/telegram)

class Script {
    addQuotePrefix(str) {
        let tmp = str.split('\n'),
            res = [];

        for (const frag of tmp) {
            res.push(`> ${frag}`);
        }

        return res.join('\n');
    }

    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 from = request.content.message.from;
        let who = from.username
        let icon_url = '/avatar/' + from.username + '.jpg'
        if(!who)  {
          if (from.first_name && from.last_name) {
            who = `${from.first_name} ${from.last_name}`
          } else if (from.first_name) {
            who = from.first_name
          } else {
            who = from.last_name
          }
          icon_url = `/avatar/${request.content.message.from.first_name}.jpg`
        }
        let body = request.content.message.text

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

        if(request.content.message.reply_to_message) {
              var quotedMessage = 
                "*" +
                request.content.message.reply_to_message.from.username +
                "*\n" +
                request.content.message.reply_to_message.text;

              quotedMessage = this.addQuotePrefix(quotedMessage);
            body = quotedMessage + '\n' + body;
        }

        return {
            content: {
                username: who,
                icon_url: icon_url,
                text: body
            }
        };
    }
}

error log:

{"level":50,"time":"2023-06-28T00:28:01.091Z","pid":1,"hostname":"9afe5c84840a","name":"Integrations","section":"Incoming WebHook","msg":"Error running Script in Trigger","integration":"Telegram_Hub","script":"function _createForOfIteratorHelperLoose(o,allowArrayLike){var it=typeof Symbol!=="undefined"&&o[Symbol.iterator]||o["@@iterator"];if(it)return(it=it.call(o)).next.bind(it);if(Array.isArray(o)||(it=_unsupportedIterableToArray(o))||allowArrayLike&&o&&typeof o.length==="number"){if(it)o=it;var i=0;return function(){if(i>=o.length)return{done:true};return{done:false,value:o[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.<br>In order to be iterable, non-array objects must have a Symbol.iterator method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++)arr2[i]=arr[i];return arr2}var Script=function(){function Script(){}var _proto=Script.prototype;_proto.addQuotePrefix=function addQuotePrefix(str){var tmp=str.split("<br>"),res=[];for(var _iterator=_createForOfIteratorHelperLoose(tmp),_step;!(_step=_iterator()).done;){var frag=_step.value;res.push("> "+frag)}return res.join("<br>")};_proto.process_incoming_request=function process_incoming_request(_ref){var request=_ref.request;if("edited_message"in request.content){request.content.message=request.content.edited_message}var from=request.content.message.from;var who=from.username;var icon_url="/avatar/"+from.username+".jpg";if(!who){if(from.first_name&&from.last_name){who="${from.first_name} ${from.last_name}"}else if(from.first_name){who=from.first_name}else{who=from.last_name}icon_url="/avatar/${request.content.message.from.first_name}.jpg"}var body=request.content.message.text;if(!body){if(request.content.message.hasOwnProperty("sticker")&&request.content.message.sticker.emoji){body=request.content.message.sticker.emoji}else{return{}}}if(request.content.message.reply_to_message){var quotedMessage=""+request.content.message.reply_to_message.from.username+"<br>"+request.content.message.reply_to_message.text;quotedMessage=this.addQuotePrefix(quotedMessage);body=quotedMessage+"<br>"+body}return{content:{username:who,icon_url:icon_url,text:body}}};return Script}();","err":{"type":"Error","message":"TypeError: Cannot read property 'from' of undefined","stack":"Error: TypeError: Cannot read property 'from' of undefined
at vm.js:11:33
at VM2 Wrapper.apply (/app/bundle/programs/server/npm/node_modules/vm2/lib/bridge.js:485:11)
at /app/bundle/programs/server/npm/node_modules/meteor/promise/node_modules/meteor-promise/fiber_pool.js:43:40"},"msg":"TypeError: Cannot read property 'from' of undefined"}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Telegram integration documentation and the process_incoming_request entry point shown in the script, then inspect the incoming request shape around the vm.js:11 error. Reproduce the Telegram failure and separately check SlackBridge; done means both integrations process their incoming requests without the reported errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.