Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom processing script for integration webhooks #2432

Merged
merged 7 commits into from
Mar 7, 2016

Conversation

rodrigok
Copy link
Member

@rodrigok rodrigok commented Mar 5, 2016

Closes #2426

  • Field to insert Process_Incoming_Request script
  • Field to insert Prepare_Outgoing_Request script
  • Field to insert Process_Outgoing_Response script
  • Execute scripts in sandbox
  • Allow Prepare_Outgoing_Request to prevent request and return a new message
  • Use Logger

@rodrigok rodrigok requested a deployment to rocket-chat-pr-2432 March 7, 2016 13:35 Pending
@rodrigok rodrigok requested a deployment to rocket-chat-pr-2432 March 7, 2016 13:36 Pending
@rodrigok rodrigok changed the title [WIP] Custom processing script for integration webhooks Custom processing script for integration webhooks Mar 7, 2016
@rodrigok
Copy link
Member Author

rodrigok commented Mar 7, 2016

LGTM

1 similar comment
@engelgabriel
Copy link
Member

LGTM

engelgabriel added a commit that referenced this pull request Mar 7, 2016
Custom processing script for integration webhooks
@engelgabriel engelgabriel merged commit eed4a65 into develop Mar 7, 2016
@engelgabriel engelgabriel deleted the improvements/webhook-sripting branch March 7, 2016 13:43
@knvpk
Copy link

knvpk commented Mar 7, 2016

Hey, how to use this filed to transform incoming data.

@ma-karai
Copy link

ma-karai commented Mar 7, 2016

I am trying to figure that out as well :D

maybe someone could point us to the right direction

@bodyParams { update_id: 1337, 
                message: {  message_id: 1337, 
                                  from: { id: 123456789, first_name: 'bott0r' }, 
                                  chat: { id: 123456789, first_name: 'bott0r', type: 'private' }, 
                                  date: 1456911431, 
                                  text: 'Hi how are you' 
                        } 
            }


needs to be to changed to 

@bodyParams {
  "text": " ",
  "username":" "
  "attachments": [
                    {
                        "title": "",
                        "title_link": "",
                        "text": "",
                        "image_url": "",
                        "color": ""
    }
  ]
}

@javaning
Copy link
Contributor

javaning commented Mar 8, 2016

I am not familiar with the vm module, for incoming web-hook, maybe write something like this ?:
here content is one key in sandbox.

var res = {"content":
{   "text": " ",
    "username":" ",
    "attachments": [
        {
            "title": "",
            "title_link": "",
            "text": "",
            "image_url": "",
            "color": ""
        }
    ]
}
}
if (headers.event=='xxxx') 
{
    res.content.text= content.xxx; //this is a message;
    res.attachments.title='1111'; //...
}

return res;

@knvpk
Copy link

knvpk commented Mar 8, 2016

Im actually using the Gitlab to integrate with, but gitlab will send event names as request header with the name X-Gitlab-Event , with these type of header name i cant write headers.X-Gitlab-Event and i also tried like this headers['X-Gitlab-Event'] but not working.

@ma-karai
Copy link

ma-karai commented Mar 8, 2016

Thank you for your kind reply, i am quite litterally on the struggle bus when it comes to read out the incoming debug log looks like this

Incoming WebHook.info Post integration 
Incoming WebHook.debug @urlParams { integrationId: 'ZhPfLSTzD4Hun9nwz',   userId: 'rocket.cat',   token: '7ETqvkRMSrs0Jrjta+Fu40o+PVCQN6jHkjPcdQb7yFA=' } 

Incoming WebHook.debug @bodyParams { update_id: 276398283,   message:     { message_id: 124,      from: { id: 9986470, first_name: 'bott0r' },      chat: { id: 9986470, first_name: 'bott0r', type: 'private' },      date: 1457431236,      text: 'Ffff' } } 

Incoming WebHook.info will execute script processIncomingRequestScript 
Incoming WebHook.debug result = (function() { var res = {"content": {   "text": "",     "username":"",     "attachments": [         {             "title": "",             "title_link": "",             "text": "",             "image_url": "",             "color": ""         }     ] } } if (headers=='content')  {    res.content.username=message.from.first_name;                          //this is a firstname;    res.content.attachments.text=content.message.text;                                   //this is a message; }  return res; }()); 
Incoming WebHook.debug with context { url:     { hash: null,      search: null,      query: {},      pathname: '/hooks/ZhPfLSTzD4Hun9nwz/rocket.cat/7ETqvkRMSrs0Jrjta+Fu40o+PVCQN6jHkjPcdQb7yFA=',      path: '/hooks/ZhPfLSTzD4Hun9nwz/rocket.cat/7ETqvkRMSrs0Jrjta+Fu40o+PVCQN6jHkjPcdQb7yFA=' },   url_raw: '/hooks/ZhPfLSTzD4Hun9nwz/rocket.cat/7ETqvkRMSrs0Jrjta+Fu40o+PVCQN6jHkjPcdQb7yFA=',   url_params:     { integrationId: 'ZhPfLSTzD4Hun9nwz',      userId: 'rocket.cat',      token: '7ETqvkRMSrs0Jrjta+Fu40o+PVCQN6jHkjPcdQb7yFA=' },   content:     { update_id: 276398283,      message:        { message_id: 124,         from: [Object],         chat: [Object],         date: 1457431236,         text: 'Ffff' } },   content_raw:     { update_id: 276398283,      message:        { message_id: 124,         from: [Object],         chat: [Object],         date: 1457431236,         text: 'Ffff' } },   headers:     { connection: 'upgrade',      host: 'bottor.com',      'x-real-ip': '1222.154.167.200',      'x-forward-for': '1422.154.167.200',      'x-forward-proto': 'http',      'x-nginx-proxy': 'true',      'content-length': '184',      'content-type': 'application/json' },   user: { _id: 'rocket.cat', name: 'Rocket.Cat', username: 'rocket.cat' } } 
Incoming WebHook.debug result { text: '',   username: '',   attachments: [ { title: '', title_link: '', text: '', image_url: '', color: '' } ] } 

@javaning
Copy link
Contributor

javaning commented Mar 8, 2016

@pavankumarkatakam try headers["X-Gitlab-Event"] ?

@javaning
Copy link
Contributor

javaning commented Mar 8, 2016

@bott0r line
res.content.username=content.message.from.first_name; //right?

please refer this Scripting a Node.js App

@ma-karai
Copy link

ma-karai commented Mar 8, 2016

I have no idea what i should be doing and the line you quoted is of course completely wrong :D

@rodrigok
Copy link
Member Author

rodrigok commented Mar 8, 2016

Hey folks, I created a tutorial to use scripting to integrate with GitHub https://github.com/RocketChat/Rocket.Chat/wiki/WebHook-Scripting---GitHub

If you create new integrations can you share with us?

@ma-karai
Copy link

ma-karai commented Mar 8, 2016

with my limited background (physicist) i doubt i can share anything anytime soon :(

@rodrigok Would you mind inserting the RAW payload that arrives from github so that your script works.

The example you provided is excellent, programming is just not my forte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants