-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (101 loc) · 7.3 KB
/
index.js
File metadata and controls
116 lines (101 loc) · 7.3 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
109
110
111
112
113
114
115
116
/**
* Text Notification
*
* Sends a text notification to the hosts of a space when a new user arrives.
*
* @license MIT
* @author zmaqutu
*/
module.exports = class TextNotificationPlugin extends BasePlugin {
/** Plugin info */
static get id() { return 'zongo.text-notify' }
static get name() { return 'Text Notification' }
static get description() { return 'Notifies space hosts via text when a new user arrives in their space.' }
phoneNumberMap = new Map()
userName = ''
/** Called when the plugin is loaded */
async onLoad() {
// Register button
this.menus.register({
id: 'zongo.text-notify',
text: 'Notify',
section: 'controls',
adminOnly: false,
order: 11,
icon: 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE4LjEuMSwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPg0KPHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJDYXBhXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB2aWV3Qm94PSIwIDAgNjExLjk5OSA2MTEuOTk5IiBzdHlsZT0iZW5hYmxlL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DEyNi45NiwzMTUuNTMyLDExOS4yMzUsMzA2LjAwMSwxMTkuMjM1eiIvPg0KCQk8L2c+DQoJPC9nPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPC9zdmc+DQo=',
action: this.onMenuPress.bind(this)
})
// Register settings
this.menus.register({
id: 'zongo.text-notify-config',
section: 'plugin-settings',
panel: {
fields: [
{ type: 'section', name: 'Phone Numbers' },
{ type: 'text', id: 'phone-number1', name: 'Phone Number', help: 'Enter the phone number you would like to be notified on when a user joins the space.<br/>For example: +1 415 555 2671' },
{ type: 'text', id: 'phone-number2', name: 'Phone Number', help: 'Enter the phone number you would like to be notified on when a user joins the space.<br/>For example: +1 415 555 2671' },
{ type: 'text', id: 'phone-number3', name: 'Phone Number', help: 'Enter the phone number you would like to be notified on when a user joins the space.<br/>For example: +1 415 555 2671' },
{ type: 'text', id: 'phone-number4', name: 'Phone Number', help: 'Enter the phone number you would like to be notified on when a user joins the space.<br/>For example: +1 415 555 2671' },
{ type: 'text', id: 'phone-number5', name: 'Phone Number', help: 'Enter the phone number you would like to be notified on when a user joins the space.<br/>For example: +1 415 555 2671' }
]
}
})
// Fetch user display name
try {
this.userName = await this.user.getDisplayName()
} catch (err) {
console.error('[TextNotification] Unable to fetch username', err)
}
}
/**
* Updates the plugin when settings have changed.
* @param {string} field Field that has been updated.
* @param {string} value New value of the field.
*/
onSettingsUpdated(field, value) {
value = value.trim()
// Clear phone number if user wishes to do so
if (value.length < 1) {
this.phoneNumberMap.delete(field)
return
}
// Remove any additional spaces or dashes
value = value.replace(/(\s|-)/g, '')
// Check that the number is of the correct format then add to the map
if (value.match(/^\+[1-9]\d{1,14}$/)) {
this.phoneNumberMap.set(field, value)
} else {
this.menus.alert('Please re-enter the number with the correct format. For example: +14155552671 or +1 415 555 2671', 'Invalid international number', 'error')
}
}
/** Called when the user presses the Notify button */
async onMenuPress() {
// No numbers to send text to
if (this.phoneNumberMap.size < 1) {
this.menus.alert('Space hosts do not wish to be notified at this moment', 'Notification not sent', 'info')
return
}
// Send message to each number
for (let number of this.phoneNumberMap.values()) {
// No associated number, so skip
if (!number) {
continue
}
// Send data in format "userName:number"
const dataToSend = this.userName + ':' + number
try {
fetch('https://us-central1-ydangle-high-fidelity-test-2.cloudfunctions.net/sendSMSNotification', {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'text/plain'
},
body: dataToSend,
})
} catch (err) {
console.error(`[TextNotification] Message to number "${number}" not sent`, err)
}
}
this.menus.alert('Space hosts have been notified of your arrival', 'Notification sent', 'success')
}
}