-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo-hangoutschat-bot.gs
160 lines (150 loc) · 4.52 KB
/
todo-hangoutschat-bot.gs
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
155
156
157
158
159
160
/**
* Responds to a MESSAGE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onMessage(event) {
console.log(event);
var userName = '';
if (event.space.type == "DM") {
userName = 'your';
} else {
userName = event.user.displayName;
}
var taskText = event.message.text.replace('@todo-bot', '').trim();
if (taskText == '') {
taskText = 'no text(only thread direct link)';
}
var taskList = getTaskList('Chat');
if (!taskList) {
// it doesn’t access Tasks, contact your G Suite administrator
var message = 'add a task to todo-list named *Chat* or default todo-list.';
return { "text": message };
}
// https://chat.google.com/room/<room-name>/<thread-name>
var threadURL = 'https://chat.google.com/room/' + event.message.thread.name.replace('spaces/', '').replace('threads/', '') ;
var task = {
title: '[' + event.space.displayName + ']' + taskText,
notes: threadURL
};
// console.log(task);
addTask(taskList.id, task);
// var message = 'added the task to todo-list named(' + *taskList.title* + ') of userName.\ntitle:' + task.title + '\nnotes:' + task.notes;
// return { "text": message };
// card format
return {
"cards": [
{
"header": {
"title": "added the task",
"subtitle": "futa23/todo-hangoutschat-bot",
"imageUrl": "https://www.gstatic.com/companion/icon_assets/tasks2_2x.png"
},
"sections": [
{
"widgets": [
{
"keyValue": {
"icon": "BOOKMARK",
"topLabel": userName + "'s todo-list ",
"content": taskList.title,
}
},
{
"keyValue": {
"icon": "DESCRIPTION",
"topLabel": "Room",
"content": event.space.displayName
}
},
{
"keyValue": {
"topLabel": "Title",
"content": taskText,
"contentMultiline": "true"
}
},
{
"keyValue": {
"topLabel": "Notes(Thread Direct Link)",
"content": task.notes,
"contentMultiline": "true"
}
}
]
},
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "Open Tasks(help)",
"onClick": {
"openLink": {
"url": "https://support.google.com/a/users/answer/9310341?hl=ja&ref_topic=9298644"
}
}
}
}
]
}
]
}
]
}
]
};
}
/**
* Responds to an ADDED_TO_SPACE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onAddToSpace(event) {
if (!event.message || !event.message.text) {
var message = 'add a task to todo-list named *Chat* or default todo-list.\n + `@todo-bot task title`';
return { "text": message };
}
console.log('[onAddToSpace]' + '<room>name=' + event.space.name + ',displayName=' + event.space.displayName + '<user>name=' + event.user.name + ',displayName=' + event.user.displayName);
return onMessage(event);
}
/**
* Responds to a REMOVED_FROM_SPACE event in Hangouts Chat.
*
* @param {Object} event the event object from Hangouts Chat
*/
function onRemoveFromSpace(event) {
console.log("[onRemoveFromSpace]Bot removed from " + '<room>name=' + event.space.name + ',displayName=' + event.space.displayName + '<user>name=' + event.user.name + ',displayName=' + event.user.displayName);
}
/**
* Adds a task to a tasklist.
* @param {string} taskListId The tasklist to add to.
* @param {Object} to post task{title, notes}
*/
function addTask(taskListId, task) {
task = Tasks.Tasks.insert(task, taskListId);
return task;
}
/**
* getTaskList by listName
*
* @param {string} taskList.title
*/
function getTaskList(listName) {
var taskLists = Tasks.Tasklists.list();
if (taskLists.items) {
for (var i = 0; i < taskLists.items.length; i++) {
var taskList = taskLists.items[i];
// return mathed taskList.title
if(listName && taskList.title == listName) {
return taskList;
}
}
}
if (listName && taskLists.items) {
// default when not matched
return taskLists.items[0];
}
return null;
}