1 | $ wsk package create myCustomSlack --param url "https://hooks.slack.com/services/..." --param username Bob --param channel "#MySlackChannel" |
1 | $ wsk action create myCustomSlack/post2slack slackPost.js |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | var request = require('request'); var utils = { map : function(arr, func){ var res = []; for(var i=0,len=arr.length; i<len; i++){ res.push(func(arr)); } return res; }, encodeStrings : function(arr){ return utils.map(arr, querystring.escape); }, decodeStrings : function(arr){ return utils.map(arr, decodeURI); }, }; /** * Action to post to slack * @param {string} url - Slack webhook url * @param {string} channel - Slack channel to post the message to * @param {string} username - name to post to the message as * @param {string} text - message to post * @param {string} icon_emoji - (optional) emoji to use as the icon for the message * @param {string} as_user - (optional) when the token belongs to a bot, whether to post as the bot itself * @param {object} attachments - (Optional) message attachments * @return {object} whisk async */ function main(params){ if (checkParams(params)){ d = params.result; console.log('Result: ', d); for(var i=0,len=d.length; i<len; i++){ d['garbled_lines'] = utils.decodeStrings(d['garbled_lines']); } console.log('Decoded Result: ', d); var body = { channel: params.channel, username: params.username || 'Simple Message Bot', text: format(JSON.stringify(d, null, '\t')) }; if (params.icon_emoji){ body.icon_emoji = params.icon_emoji; } if (params.token){ // // this allows us to support /api/chat.postMessage // e.g. users can pass params.url = https://slack.com/api/chat.postMessage // and params.token = \u003ctheir auth token\u003e // body.token = params.token; } else { // // the webhook api expects a nested payload // // Notice that we need to stringify; this is due to limitations // of the formData npm: it does not handle nested objects // console.log(body); console.log("to: "+ params.url); body = { payload: JSON.stringify(body) }; } if (params.as_user === true){ body.as_user = true; } if (params.attachments) { body.attachments = params.attachments; } var promise = new Promise(function(resolve, reject){ request.post({ url: params.url, formData: body, }, function(err, res, body) { if (err) { console.log('Error: ', err, body); reject(err); } else { console.log('Success: ', params.text, ' successfully sent'); resolve(); } }); }); return promise; } } /** * * Checks if all required params are set */ function checkParams(params){ console.log('Post2Slack params: ', params); if (params.result === undefined) { whisk.error('No post data provided'); return false; } if (params.url === undefined) { whisk.error('No webhook URL provided'); return false; } if (params.channel === undefined) { whisk.error('No channel provided'); return false; } return true; } /** * format text to slack */ function format(str){ return '\n\`\`\`'+str+'\n\`\`\`'; } |
1 | $ wsk action create myCustomSlack/post2slack slackPost.js |
1 | $ wsk action create git2slack --sequence garbageDetectionAction,myCustomSlack/post2slack |
1 | $ wsk action invoke git2slack --blocking --result -p payload |
1 | $ wsk action update git2slack –sequence getPushPayloadAction,garbageDetectAction,myCustomSlack/post2slack |
1 2 3 | $ wsk rule disable git2slackRule $ wsk rule update git2slackRule gitTrigger git2slack $ wsk rule enable git2slackRule |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |