-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineBot.js
More file actions
108 lines (97 loc) · 3.11 KB
/
Copy pathlineBot.js
File metadata and controls
108 lines (97 loc) · 3.11 KB
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
let request = require('request-promise');
let config = require('./config');
let co = require('co');
let Promise = require('bluebird');
/*
* 這兩個值是固定的,一定要固定唷
*/
const eventType = '138311608800106203';
const toChannel = '1383378250';
module.exports = {
/*
* Line channal 傳過來的資料
*/
callback: function(req, res, next) {
// 1. 處理 line 傳過來的訊息
let data = req.body.result[0]; // 從 channal post 過來的資料
let text = data.content.text || '這不是文字訊息 Q____________Q'; // 得到的訊息資料
let contentType = data.content.contentType; // 傳過來的資料類型
let fromWho = data.content.from; // 誰傳過來的
console.log(data);
console.log('------------------------');
// 2. 分辨不同的內容
let content;
switch(contentType) {
case 1:
content = {
toType: 1,
contentType: 1,
text: '你傳文字訊息給我'
};
break;
case 2:
content = {
toType: 1,
contentType: 1,
text: '你傳照片給我'
};
break;
case 8:
content = {
toType: 1,
contentType: 1,
text: '你傳貼圖給我'
};
break;
default:
content = {
toType: 1,
contentType: 1,
text: '我現在分別不出來你傳什麼碗糕給我'
};
}
// 3. 組成回傳的物件
let options = {
method: 'POST',
uri: 'https://trialbot-api.line.me/v1/events',
body: {
to: [fromWho],
toChannel: toChannel,
eventType: eventType,
content: content
},
headers: {
'Content-Type': 'application/json; charser=UTF-8',
'X-Line-ChannelID': config.line.channelId,
'X-Line-ChannelSecret': config.line.channelSecret,
'X-Line-Trusted-User-With-ACL': config.line.channelMID
},
json: true
};
// Slack webhook
let slackOptions = {
method: 'POST',
uri: config.slack.webhook,
body: JSON.stringify({text: text}),
headers: {
'Content-Type': 'application/json; charser=UTF-8'
},
};
co(function*() {
let results = yield [
request(options),
request(slackOptions)
];
return Promise.resolve(results);
})
.then(function(results) {
console.log(results[0]);
console.log(results[1]);
return res.status(200).send();
})
.catch(function(err) {
console.log(err);
return res.status(400).send();
});
}
};