This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ideaBot.js
131 lines (113 loc) · 3.57 KB
/
ideaBot.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
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
require('dotenv').config()
const YAML = require('yaml')
const fs = require('fs')
const Jimp = require('jimp')
const Twitter = require('twitter')
const Twit = require('twit')
const fileEvents = fs.readFileSync('./events.yml', 'utf8')
const actionsEvents = YAML.parse(fileEvents)
const fileConsequences = fs.readFileSync('./consequences.yml', 'utf8')
const actionsConsequences = YAML.parse(fileConsequences)
// Load environment variables
const consumerKey = process.env.TWITTER_CONSUMER_KEY
const consumerSecret = process.env.TWITTER_CONSUMER_SECRET
const accessTokenKey = process.env.TWITTER_ACCESS_TOKEN_KEY
const accessTokenSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET
exports.newIdea = (req, res) => {
run()
async function run() {
let actions = await chooseActions()
let image = await createImage(actions)
const postSuccess = await postToTwitter(image, actions)
if (!postSuccess) {
console.log(`Didn't work out this time.`)
} else {
console.log('Upload complete.')
}
return res.status(200).send('Message received')
}
async function chooseActions() {
// pick two random actions from the lists
let actionOne =
actionsConsequences[
Math.floor(Math.random() * actionsConsequences.length)
]
let actionTwo =
actionsEvents[Math.floor(Math.random() * actionsEvents.length)]
return [actionOne, actionTwo]
}
async function createImage(actions) {
let image, newImage
let text = `An app that ${actions[0]} when somebody ${actions[1]}.`
// choose an image from the files in /assets/images
let imageNames = []
fs.readdirSync('images').forEach(file => {
imageNames.push(file)
})
const imageName = imageNames[Math.floor(Math.random() * imageNames.length)]
image = await Jimp.read(`images/${imageName}`)
newImage = await image.clone()
await newImage.resize(600, Jimp.AUTO)
let x = 10
let y = 10
let font = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE)
await newImage.print(
font,
x,
y,
{
text: text,
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER
},
580,
110
)
return newImage
}
async function postToTwitter(image, actions) {
const client = new Twit({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
let imageID = await uploadImage(client, image)
if (imageID) {
try {
let altText = `An app that ${actions[0]} when somebody ${actions[1]}.`
await client.post('media/metadata/create', {
media_id: imageID,
alt_text: { text: altText }
})
} catch (err) {
console.log(err)
return false
}
try {
return await client.post('statuses/update', { media_ids: [imageID] })
} catch (err) {
console.log(err)
return false
}
} else {
console.log(`Image upload failed`)
return false
}
}
async function uploadImage(client, image) {
const mime = image.getMIME()
const base64content = await image.getBase64Async(mime)
const stripIt = 'data:image/png;base64,'
const imgSrcString = base64content.replace(stripIt, '')
try {
// upload media to twitter
const responseData = await client.post('media/upload', {
media_data: imgSrcString
})
return responseData.data.media_id_string
} catch (err) {
console.log(err)
return false
}
}
}