This commit is contained in:
ekardnam 2019-07-26 18:55:25 +02:00
parent 9869f60213
commit 0649ade820
2 changed files with 14 additions and 13 deletions

2
app.js
View file

@ -16,7 +16,7 @@ let main = () => {
}
if (storage.data.token && storage.data.token_secret) {
console.log("[INFO] Starting bot")
console.log('[INFO] Starting bot')
bot.start()
} else {
oauth.start()

25
bot.js
View file

@ -57,6 +57,7 @@ let bot = {
// Video 15MB
// medias is an array of filenames
twitterPostWithMedias: function (post, medias) {
let that = this
let pieces = this.splitForImage(post)
let uploads = []
@ -65,7 +66,7 @@ let bot = {
let b64content = readBase64(media)
uploads.push(
new Promise((resolve, reject) => {
this.twitter.post('media/upload', { media_data: b64content }, (err, data, response) => {
that.twitter.post('media/upload', { media_data: b64content }, (err, data, response) => {
if (err) reject(err)
else resolve(data)
})
@ -75,19 +76,18 @@ let bot = {
return Promise.all(uploads).then(datas => {
let mediaIds = datas.map(data => data.media_id_string)
let altText = 'test'
mediaIds.forEach(mediaId => {
this.twitter.post('media/metadata/create', { media_id: mediaId, alt_text: { text: altText } }, (err, data, response) => {
return new Promise((resolve, reject) => {
that.twitter.post('statuses/update', { status: pieces[0], media_ids: mediaIds }, (err, data, response) => {
if (!err) {
this.twitter.post('statuses/update', { status: pieces[0], media_ids: mediaIds }, (err, data, response) => {
if (!err) {
console.log(`[INFO] Tweeted ${post} with ${medias}`)
if (pieces.length > 1) this.twitterPost(pieces[1], data.id_str)
} else throw err
})
} else throw err
resolve(data)
} else reject(err)
})
})
}).then((data) => {
if (pieces.length > 1) return that.twitterPost(pieces[1], data.id_str)
else return Promise.resolve()
}).then(() => {
console.log(`[INFO] Tweeted ${post} with ${medias}`)
})
},
@ -111,7 +111,7 @@ let bot = {
promise = promise.then(data => this.promisePost(pieces[i], data.id_str))
}
promise.then(() => console.log(`[INFO] Tweeted ${post}`))
return promise.then(() => console.log(`[INFO] Tweeted ${post}`))
.catch(err => console.error(err))
},
@ -138,6 +138,7 @@ let bot = {
.replace(/<br\/>/g, '\n')
.replace(/<\/p>/g, '\n')
.replace(/&apos;/g, '\'')
.replace(/&quot;/g, '"')
.replace(/<[^>]*>?/gm, '')
},