Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created July 18, 2017 22:39
Show Gist options
  • Save jcipriano/49fa8f6efb775a8835740e3d73bd0a7e to your computer and use it in GitHub Desktop.
Save jcipriano/49fa8f6efb775a8835740e3d73bd0a7e to your computer and use it in GitHub Desktop.
Sends a Twitter DM with Quick Reply Options and Buttons.
var nconf = require('nconf')
var request = require('request')
// load config
nconf.file({ file: 'config.json' }).env()
// twitter authentication
var twitter_oauth = {
consumer_key: nconf.get('TWITTER_CONSUMER_KEY'),
consumer_secret: nconf.get('TWITTER_CONSUMER_SECRET'),
token: nconf.get('TWITTER_ACCESS_TOKEN'),
token_secret: nconf.get('TWITTER_ACCESS_TOKEN_SECRET')
}
// direct message request body
var dm_params = {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": "4534871"
},
"message_data": {
"text": "What color bird is your fav?",
"ctas": [
{
"type": "web_url",
"label": "Go to a website",
"url": "https://dev.twitter.com"
},
{
"type": "web_url",
"label": "Go to another website",
"url": "https://dev.twitter.com"
}
],
"quick_reply": {
"type": "options",
"options": [
{
"label": "Go back to home",
"description": "A description",
"metadata": "external_id_1"
},
{
"label": "Do something else",
"description": "A description",
"metadata": "external_id_2"
}
]
}
}
}
}
}
// request options
var request_options = {
url: 'https://api.twitter.com/1.1/direct_messages/events/new.json',
oauth: twitter_oauth,
json: true,
headers: {
'content-type': 'application/json'
},
body: dm_params
}
// POST request to send Direct Message
request.post(request_options, function (error, response, body) {
console.log(body)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment