diff --git a/app.js b/app.js index 99a42cb..2fe121d 100644 --- a/app.js +++ b/app.js @@ -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() diff --git a/bot.js b/bot.js index 28eb015..e347708 100644 --- a/bot.js +++ b/bot.js @@ -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(//g, '\n') .replace(/<\/p>/g, '\n') .replace(/'/g, '\'') + .replace(/"/g, '"') .replace(/<[^>]*>?/gm, '') },