-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
158 lines (136 loc) · 5.22 KB
/
Code.gs
File metadata and controls
158 lines (136 loc) · 5.22 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
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
function setScriptProperties() {
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperties({
SKILL_IDS: JSON.stringify([
{ "id": "skill-id-1" },
{ "id": "skill-id-2", "target": "target-user-id" }
]),
SPECIFIC_REWARD_ID: "your-custom-reward-id",
SPECIFIC_REWARD_TEXT: "Your Custom Reward"
USER_ID: "your-habitica-user-id"
API_TOKEN: "your-habitica-api-token"
});
}
function doPost(e) {
// Log that the script has been triggered
Logger.log('Script triggered');
// Load environment variables from Script Properties with error handling
var scriptProperties = PropertiesService.getScriptProperties();
var skillIdsProperty = scriptProperties.getProperty('SKILL_IDS');
if (!skillIdsProperty) throw new Error('SKILL_IDS is not defined in Script Properties.');
var skillObjects;
try {
skillObjects = JSON.parse(skillIdsProperty);
} catch (error) {
throw new Error('SKILL_IDS is not a valid JSON string.');
}
var specificRewardId = scriptProperties.getProperty('SPECIFIC_REWARD_ID');
if (!specificRewardId) throw new Error('SPECIFIC_REWARD_ID is not defined.');
var specificRewardText = scriptProperties.getProperty('SPECIFIC_REWARD_TEXT');
if (!specificRewardText) throw new Error('SPECIFIC_REWARD_TEXT is not defined.');
var userId = scriptProperties.getProperty('USER_ID');
if (!userId) throw new Error('USER_ID is not defined.');
var apiToken = scriptProperties.getProperty('API_TOKEN');
if (!apiToken) throw new Error('API_TOKEN is not defined.');
// Check if the POST data exists
if (!e.postData || !e.postData.contents) {
Logger.log('No post data received');
return ContentService.createTextOutput(JSON.stringify({ status: 'no data received' }))
.setMimeType(ContentService.MimeType.JSON);
}
// Parse the JSON data from Habitica
var data;
try {
data = JSON.parse(e.postData.contents);
Logger.log('Received data: ' + JSON.stringify(data));
} catch (error) {
Logger.log('Error parsing JSON: ' + error);
return ContentService.createTextOutput(JSON.stringify({ status: 'invalid JSON' }))
.setMimeType(ContentService.MimeType.JSON);
}
// Verify that the data is the expected webhook event
if (data.type === 'scored' && data.task && data.task.type === 'reward') {
var task = data.task;
// Check if the purchased reward matches your specific reward
if (task.id === specificRewardId || task.text === specificRewardText) {
Logger.log('Specific reward purchased: ' + task.text);
bulkCastSkills(userId, apiToken, skillObjects);
} else {
Logger.log('Different reward purchased: ' + task.text);
}
} else {
Logger.log('Event is not a reward purchase or has unexpected format');
}
// Return a success response
return ContentService.createTextOutput(JSON.stringify({ status: 'success' }))
.setMimeType(ContentService.MimeType.JSON);
}
function bulkCastSkills(userId, apiToken, skillObjects) {
// Base URL for casting skills
var baseUrl = 'https://habitica.com/api/v3/user/class/cast/';
Logger.log(skillObjects);
// Headers for authentication
var headers = {
'x-client' : 'eef575f7-d8ac-4707-ad41-6ee9b96b681' + ' - ' + 'Habitica Custom Reward Trigger',
'x-api-user': userId,
'x-api-key': apiToken,
'Content-Type': 'application/json'
};
// Check if skillObjects is an array and not empty
if (!Array.isArray(skillObjects) || skillObjects.length === 0) {
Logger.log('No skill IDs provided to cast.');
return;
}
// Iterate over the skill objects and cast each one
skillObjects.forEach(function(skill) {
try {
// Construct the endpoint URL for the skill
var url = baseUrl + encodeURIComponent(skill.id);
// Prepare the options for UrlFetchApp.fetch
var options = {
method: 'post',
headers: headers,
muteHttpExceptions: true // To handle errors gracefully
};
// If the skill requires a target, add the targetId to the payload
if (skill.target) {
options.payload = JSON.stringify({ targetId: skill.target });
}
Logger.log(url);
Logger.log(options);
// Make the API request to cast the skill
var response = UrlFetchApp.fetch(url, options);
// Parse the response
var result = JSON.parse(response.getContentText());
if (result.success) {
Logger.log('Successfully cast skill: ' + skill.id);
} else {
Logger.log('Failed to cast skill: ' + skill.id + '. Error: ' + result.message);
}
} catch (error) {
Logger.log('Error casting skill: ' + skill.id + '. Exception: ' + error);
}
});
}
function testDoPost() {
// Create a simulated event object 'e'
var e = {
postData: {
contents: JSON.stringify({
type: 'scored',
task: {
id: 'f1463b2c-5ae2-4fce-b59d-c64e3a340111', // Ensure this matches your SPECIFIC_REWARD_ID
text: 'Test', // Ensure this matches your SPECIFIC_REWARD_TEXT
type: 'reward'
}
}),
length: 0,
type: 'application/json',
name: 'postData'
}
};
// Call the doPost function with the simulated event
var response = doPost(e);
// Log the response
Logger.log('Response: ' + response.getContent());
}