-
Notifications
You must be signed in to change notification settings - Fork 1
/
hangouts.js
154 lines (127 loc) · 3.67 KB
/
hangouts.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const { existsSync } = require('fs');
const { google } = require('googleapis');
const jsonfile = require('jsonfile');
const sessionStorage = './.sessions_hangouts';
async function buildHangoutBtns(qr) {
const result = [];
qr.forEach((e) => {
result.push({
textButton: {
text: e.title,
onClick: {
action: {
actionMethodName: 'quickreply',
parameters: [
{
key: 'payload',
value: e.payload,
},
// {
// key: 'id',
// value: '123456',
// },
],
},
},
},
});
});
return result;
}
function buildEvent(chatMessage) {
const event = {
rawEvent: {
recipient: {
id: process.env.PAGE_ID,
},
},
};
const { type } = chatMessage;
if (type === 'MESSAGE') {
event.isText = true;
event.message = { text: chatMessage.message.argumentText };
} else if (type === 'CARD_CLICKED') {
const { action } = chatMessage;
if (action && action.actionMethodName === 'quickreply') {
event.isQuickReply = true;
event.quickReply = { payload: action.parameters[0].value };
}
} else if (type === 'ADDED_TO_SPACE') {
event.isPostback = true;
event.postback = { payload: 'greetings' };
} else if (type === 'ADDED_TO_SPACE') {
event.isPostback = true;
event.postback = { payload: 'end' };
}
return event;
}
class HangoutContext {
constructor(chatMessage) {
this.scopes = 'https://www.googleapis.com/auth/chat.bot';
this.message = chatMessage;
this.parent = chatMessage.space.name;
this.event = buildEvent(chatMessage);
this.platform = 'hangouts';
this.session = { user: { id: this.message.user.name.replace('users/', '') } };
this.state = {};
this.pathToState = `${sessionStorage}/${this.session.user.id}.json`;
}
async build() {
try {
this.auth = await google.auth.getClient({ scopes: this.scopes });
this.chat = google.chat({ version: 'v1', auth: this.auth });
const { pathToState } = this;
if (await existsSync(pathToState)) { // read from the state file if it exists
const file = await jsonfile.readFileSync(pathToState);
this.state = file;
} else { // create default state file if it doesn't exist
jsonfile.writeFileSync(pathToState, this.state);
}
} catch (err) {
console.error(err);
}
}
async setState(newState) {
this.state = { ...this.state, ...newState };
await jsonfile.writeFileSync(this.pathToState, this.state); // update state file
}
async sendText(text, qr) {
if (!text || typeof text !== 'string') return false;
await this.chat.spaces.messages.create({
parent: this.parent,
requestBody: {
text: `${text}\n\n`,
},
});
if (qr && qr.quick_replies) {
const buttons = await buildHangoutBtns(qr.quick_replies);
await this.chat.spaces.messages.create({
parent: this.parent,
requestBody: {
cards: [{ sections: [{ widgets: [{ buttons }] }] },
],
},
});
}
return true;
}
async sendImage(url) { await this.sendText(url); }
async sendVideo(url) { await this.sendText(url); }
async sendAudio(url) { await this.sendText(url); }
async sendFile(url) { await this.sendText(url); }
async typing() { return null; } // eslint-disable-line class-methods-use-this
async typingOn() { return null; } // eslint-disable-line class-methods-use-this
async typingOff() { return null; } // eslint-disable-line class-methods-use-this
async getUserProfile() {
const { user } = this.message;
const profile = {
name: user.displayName,
profilePic: user.avatarUrl,
email: user.email,
firstName: user.displayName.substr(0, user.displayName.indexOf(' ')),
lastName: user.displayName.substr(user.displayName.indexOf(' ') + 1),
};
return profile;
}
}
module.exports = HangoutContext;