-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
109 lines (89 loc) · 2.82 KB
/
handler.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
'use strict';
// Import dependencies
const DynamoDB = require('aws-sdk/clients/dynamodb');
const { DynamoBotStorage } = require('botbuilder-dynamodb-storage');
const client = new DynamoDB({ region: "ap-northeast-1" });
// Define the adapter settings
const settings = {
// Required
tableName: process.env.DYNAMODB_TABLE,
// Required
primaryKey: "id",
// Optional but strongly recommended!
ttl: {
userData: 3600 * 24 * 365, // a year,
conversationData: 3600 * 24 * 7, // a week,
privateConversationData: 3600 * 24 * 7
}
};
// Instantiate the adapter with the client and settings.
const adapter = new DynamoBotStorage(client, settings)
var config = require("./conf");
// console.log("config", config)
var bot_dailog = require("./bot/bot")
var botbuilder_linebot_connector_1 = require("botbuilder-linebot-connector");
// var botbuilder_mongodb_storage_1 = require("botbuilder-mongodb-storage");
var builder = require('botbuilder');
var connector = new botbuilder_linebot_connector_1.LineConnector({
hasPushApi: true,
// your line
channelId: process.env.channelId || config.channelId,
channelSecret: process.env.channelSecret || config.channelSecret,
channelAccessToken: process.env.channelAccessToken || config.channelAccessToken
});
var bot = new builder.UniversalBot(connector).set('storage', adapter);
bot_dailog.default(bot)
module.exports.line = (event, context, callback) => {
console.log("event", event)
connector.serverlessWebhock(event)
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'line webhock!',
input: event,
}),
};
callback(null, response);
};
module.exports.notify = (event, context, callback) => {
let body = JSON.parse(event.body);
let LineId = body.LineId;
let title = body.title
let description = body.description;
// let startTime = body.startTime;graph
let item = {
TableName: process.env.DYNAMODB_TABLE,
Key: { ["id"]: { S: LineId } },
};
client.getItem(item, (err, doc) => {
if (err) {
return reject(err);
}
let docItem = doc && doc.Item || {};
let addressString = docItem.address && docItem.address.S && JSON.parse(docItem.address.S) || {};
console.log("addressString", addressString)
if (addressString.conversation) {
// console.log("addressString", addressString)
var msg = new builder.Message().address(addressString);
msg.text(title + " " + description);
bot.send(msg);
let response = {
statusCode: 200,
body: JSON.stringify({
message: 'done',
input: event,
}),
};
callback(null, response);
} else {
let response = {
statusCode: 400,
body: JSON.stringify({
message: 'can not find user!',
input: event,
}),
};
callback(null, response);
}
})
};