-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparrot-bot.js
99 lines (85 loc) · 2.62 KB
/
parrot-bot.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
/**********************************************************************
* Parrot Bot
*
* A simple script created to deploy a Google Chat bot that repeats
* what you message it.
*
* Created by: E.Cope (Last edit: 2/6/21)
**********************************************************************
*/
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} e -- the event object from Google Chat.
*/
const onMessage = (e) => {
let name = ""
// [CASE] Direct Message -->
if (e.space.type == "DM") {
name = "You"
}
// [CASE] Space/Room/Group Chat Message -->
else {
// Grab the user's name.
name = e.user.displayName
// Split into two parts: [firstname, lastname]
name = name.split(" ")
// Use only the first name.
name = name[0]
}
// The message with all bot mentions stripped out.
let words = e.message.argumentText
// Format the response message:
let message = `${name} said: "${words.trim()}"`
// Send the message to chat.
return { "text": message }
}
//[END -- onMessage]
/**
* Responds to an ADD_TO_SPACE event in Google Chat.
*
* @param {Object} e -- the event object from Google Chat.
*/
const onAddToSpace = (e) => {
let msg = ""
let message = ""
// [CASE] Direct Message (1 on 1 convo) -->
if (e.space.singleUserBotDm) {
// Grab the user's name.
let fullName = e.user.displayName
// Split into [firstname, lastname].
let names = fullName.split(" ")
// Form the greeting using only first name.
msg = `Hey ${names[0]}! Thanks for the DM! 🦜`
}
// [CASE] Space/Room/Group Chat -->
else {
// Grab space's name -or- apply default: "the chat".
const spaceName = (e.space.displayName ? e.space.displayName : "the chat")
// Form the greeting message.
msg = `Thanks for adding me to ${spaceName}! I'll copy everything you say! 🦜`
}
// Bot @mention -->
if (e.message) {
// The message with all bot mentions stripped out.
let words = e.message.argumentText
// Form the whole reply message with greeting + repeat.
message = `${msg}
You said: "${words.trim()}"`
}
// Send the message to chat.
return { "text": message }
}
//[END -- onAddToSpace]
/**
* Responds to a REMOVE_FROM_SPACE event in Google Chat.
*
* @param {Object} e -- the event object from Google Chat.
*/
const onRemoveFromSpace = (e) => {
// Grab space's name -or- apply a default.
const spaceName = (e.space.name ? e.space.name : "the chat")
// Print the message to the console.
console.info(`Parrot Bot was removed from ${spaceName}.`)
}
//[END -- onRemoveFromSpace]