-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroupmeClient.js
63 lines (56 loc) · 2.07 KB
/
GroupmeClient.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
const DEFAULT_BOT_NAME = "New Bot";
const DEFAULT_BOT_AVATAR_URL = "https://em-content.zobj.net/thumbs/160/apple/354/robot_1f916.png";
module.exports = class GroupmeClient {
#apiToken;
constructor(apiToken) {
this.#apiToken = apiToken;
}
async makeBot(groupId, name, avatarURL, callbackURL, dmNotification=true, active=true) {
const url = `https://api.groupme.com/v3/bots?token=${this.#apiToken}`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
bot: {
group_id: groupId,
name: name || DEFAULT_BOT_NAME,
avatar_url: avatarURL || DEFAULT_BOT_AVATAR_URL,
callback_url: callbackURL,
dm_notification: dmNotification,
active,
},
}),
});
return response.json();
}
async downloadAndUploadImage(url) {
const downloadResponse = await fetch(url);
const blob = await downloadResponse.blob();
const uploadResponse = await fetch('https://image.groupme.com/pictures', {
method: 'POST',
headers: {
'X-Access-Token': this.#apiToken,
'Content-Type': downloadResponse.headers.get('Content-Type'),
},
body: blob,
});
const uploadResponseData = await uploadResponse.json();
return uploadResponseData.payload.url; // or .picture_url
}
async getGroups() {
const url = `https://api.groupme.com/v3/groups?token=${this.#apiToken}`;
const response = await fetch(url);
return response.json();
}
async sendBotMessage(botId, text, attachments=[]) {
const url = `https://api.groupme.com/v3/bots/post?token=${this.#apiToken}`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
bot_id: botId,
text,
attachments,
}),
});
return response.status === 201;
}
}