Skip to content

Instantly share code, notes, and snippets.

@andrewmacheret
Created September 20, 2017 01:56
Show Gist options
  • Select an option

  • Save andrewmacheret/b374ef439f0c7c1c622dce9d5df448f7 to your computer and use it in GitHub Desktop.

Select an option

Save andrewmacheret/b374ef439f0c7c1c622dce9d5df448f7 to your computer and use it in GitHub Desktop.
AWS Lambda function to tweet to use a specified twitter account and tweet the contents of a URL with it
const Twitter = require('lwt')
const request = require('request')
//const {promisify, callbackify} = require('util')
const promisify = require('es6-promisify')
const callbackify = require('callbackify')
const tweet = async (twitterSettings, tweetText) => {
// the following twitter settings are required:
// {
// "accessToken": "...",
// "accessSecret": "...",
// "consumerKey": "...",
// "consumerSecret": "..."
// }
const twitter = new Twitter(twitterSettings)
// promisify twitter.post
const postTwitterStatus = promisify(twitter.post.bind(twitter))
// post tweetText on twitter
const result = await postTwitterStatus('statuses/update', { status: tweetText })
console.log('tweet result', result)
if (result.errors && result.errors.length > 0 && result.errors[0].message) {
throw result.errors[0].message
}
return result
}
const tweetEvent = async (event, context) => {
if (!event.twitter) {
throw 'Could not handle the event, required property "twitter" is missing'
}
// obtain the text to post
let text
if (event.text) {
text = event.text
} else if (event.request) {
text = (await promisify(request)(event.request)).body
} else {
throw 'Could not handle the event, expected property "text" or "request"'
}
// ensure the text is actually a string
console.log('text:', text)
if (typeof text !== 'string') {
throw '"text" is not a string'
}
// tweet the text
return await tweet(event.twitter, text)
}
// export both versions of the tweetEvent method
exports.tweetEvent = tweetEvent
exports.tweetEventCallback = callbackify(tweetEvent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment