首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

基于 OpenWhisk 实时监测静态网页是否有乱码实例-2

基于 OpenWhisk 实时监测静态网页是否有乱码实例-2

自定义Slack Post Action将检测结果发送到Slack指定的Channel OpenWhisk Catalog自带了Slack Post Action,但是自带的Slack Post                Action功能有限,不能满足本实例的需求,所以需要自定义Slack Post方法来发送检测结果到Slack                Channel。自定的基本步骤如下:
  • 为您的团队创建一个Slack并且配置Slack,配置好Slack之后,可以拿到类似如下的 Webhook URL:
  • 用生成的 Webhook URL 和对应的Channel创建一个 Package
    1
    $ wsk package create myCustomSlack --param url "https://hooks.slack.com/services/..." --param username Bob --param channel "#MySlackChannel"




  • 需要写一个函数接收 Garbage Char Detection Action 传来的数据,然后发送到 Slack ,并用该函数来创建                    post2slack Action
    1
    $ wsk action create myCustomSlack/post2slack slackPost.js





    其中内容如下:
代码清单4.                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\`\`\`';
}




  • 将 post2slack action 加入到 git2slack Sequence
1
$ wsk action create myCustomSlack/post2slack slackPost.js





为了调试方便,我们没有让GitHub Trigger来触发,还是先手动传入测试数据
1
$ wsk action create git2slack --sequence garbageDetectionAction,myCustomSlack/post2slack




测试git2slack序列:
1
$ wsk action invoke git2slack --blocking --result -p payload




,               
输出结果如图8:
图8.                触发git2slack action结果当一切调试通过之后,我们在更新git2slack                序列,将getPushPayloadAction加入到git2slack序列,并更新git2slackRule.
通过以下命令更新git2slack序列:
1
$ wsk action update git2slack –sequence getPushPayloadAction,garbageDetectAction,myCustomSlack/post2slack




通过如下命令集更新git2slackRule:
1
2
3
$ wsk rule disable git2slackRule
$ wsk rule update git2slackRule gitTrigger git2slack
$ wsk rule enable git2slackRule




完成以上步骤之后,我们更新测试应用程序中的文档garbledUTF8-2.html,然后部署到Bluemix平台,并且push到GitHub                Repository,这时我们在Slack会收到如下通知:
图9.                Git仓库更新触发git2slck action图如图10,我们可以看到所有的Unicode在Slack上都显示"?".经过查看源代码,发现OpenWhisk只支持ASCII,并不支持Unicode.
图10.                Unicode字符显示异常图解决方案:通过提供的方法,我们可以在OpenWhisk处理或传输Unicode之前,将Unicode进行编码,然后在发送Unicode到Slack的Action中进行解码就可以让Unicode字符正确的在Slack上显示。相应代码修改如下:
更改
1
2
3
4
for(var i=0; i<ret.length; i++)
{
    ret['garbled_lines'] = utils.encodeStrings(ret['garbled_lines']);
}




更改
1
2
3
for(var i=0,len=d.length; i<len; i++){
d['garbled_lines'] = utils.decodeStrings(d['garbled_lines']);
}




其中,utils方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 garbageDetectionAction,myCustomSlack/post2slack,                以及git2slack sequence。然后,按照测试步骤,更新garbage-test-app某个文件,然后deploy到Bluemix,再push到GitHub                Repository,可以在Slack收到正常的返回结果,如图11所示:
图11. Unicode显示正常图
返回列表