-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (71 loc) · 1.79 KB
/
index.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
"use strict";
const request = require("request");
module.exports = function (context, callback) {
// Retrieve and prepare the user's message
const message = context.query.message || "You Done Messed Up A-Aron!";
const spongifiedString = spongify(message);
// ImgFlip API setup
const imgFlipUsername = context.secrets.imgFlipUsername || "";
const imgFlipPassword = context.secrets.imgFlipPassword || "";
// Generate the meme
getImage(imgFlipUsername, imgFlipPassword, spongifiedString, function (err, body) {
if (body) {
callback(null, body);
} else if (err) {
callback(null, "OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! Here's the deetyweetys: "+ err);
} else {
callback(null, "OOPSIE WOOPSIE!! Uwu We made a fucky wucky!!");
}
});
}
function getImage(username, password, message, callback) {
// Prepare request
const data = {
template_id: 102156234,
username,
password,
boxes: [
{
text: "", // Top Text Box Empty
},
{
text: message,
},
],
};
// Send request to API
request.post(
{
url: "https://api.imgflip.com/caption_image",
form: data,
},
function (err, httpResponse, body) {
if (err) return callback(err);
var bodyObj = {};
try {
bodyObj = JSON.parse(body);
} catch (e) {
return callback(e);
}
if (!bodyObj.success) return callback(bodyObj.error_message);
callback(null, bodyObj.data.url);
}
);
}
function spongify(message) {
let spongified = "";
let upper = true;
// only letters, not spaces etc.
let regexp = /[a-zA-Z]/;
for (let i of message) {
if (!upper && i.match(regexp) !== null) {
i = i.toLowerCase();
upper = true;
} else if (i.match(regexp) !== null) {
i = i.toUpperCase();
upper = false;
}
spongified += i;
}
return spongified;
}