Building webhooks with NodeJS

webhooks

#1

Hello. We would like if there is a simple step by step guide on how to build a webhooks to get the quickreplays DMs.
Yours Pedro


#2

Here is a quick getting started guide. We are working on a more detailed tutorial type article that should arrive in a couple weeks with code samples in NodeJS.

https://dev.twitter.com/webhooks/getting-started


#3

Thanks for your replay. We have tried al the instructions but we have been unable to register the webhook url.

I will be looking forward the nodejs sample

Yours Pedro


#4

@pazpurua here is an example of creating a webhook config with NodeJS.


#5

Hello jon. I have seen that you have already sent the new beta DM with the twit npm. Does it works?
Also you have initialize a webbhook.
I imagined that that was the way to do it. Without suceess.

So my last doubt. Have you being able to receive a replay of a betaDM at the webhook. Is it simple?

Thanks for you information Yours Pedro


#6

Here is a NodeJS webapp using express with example scripts and directions for configuring your webhook.


#7

Jon great work.
In one week or to two I will start to create the node project.

Yours Pedro


#8

I am starting from zero. But I have a first question: Where do i get the WEBHOOK_ID?
Yours Pedro


#9

First step done: I have sent the new DM

postDM: function (DM,callback){
        var twitter_oauth = {
            consumer_key: config.consumer_key,
            consumer_secret: config.consumer_secret,
            token: config.access_token,
            token_secret: config.access_token_secret
        }
        var gsilva = "160334298"
        var pazpurua = "45031619"
        // direct message request body
        var dm_params = {
            "event": {
                "type": "message_create",
                "message_create": {
                    "target": {
                        "recipient_id": pazpurua
                    },
                    "message_data": {
                        "text": "Roberto Bienvenido al la lucha. Verifiquemos Sus Datos de Registro?",
                        "quick_reply": {
                            "type": "options",
                            "options": [
                                {
                                    "label": "Cedula",
                                    "description": "Es su Cedula V3883736",
                                    "metadata": "external_id_1"
                                },
                                {
                                    "label": "Correo",
                                    "description": "Es su correo el rpiconh@unidad.org",
                                    "metadata": "external_id_2"
                                },
                                {
                                    "label": "Celular",
                                    "description": "El celular 0412 2332 3232 es el suyo?.",
                                    "metadata": "external_id_3"
                                },
                                {
                                    "label": "Telefono",
                                    "description": "El Telefono 0212 929292929 es el suyo?.",
                                    "metadata": "external_id_4"
                                }
                            ]
                        }
                    }
                }
            }
        }

        // 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) {
            var ppa = body;
            console.log(body)
            callback(null, body);
        })
    },

#10

Hey @pazpurua,

The webhook ID is returned from POST account_activity/webhooks or the following node script:

If you create a webhook config, but didn’t note the config ID, you can retrieve it with GET account_activity/webhooks or the this node script:


#12

oooops
I received the error
in the setup:

"{“errors”:[{“code”:214,“message”:"Webhook URL does not meet the requirements. Please consult: https://dev.twitter.com/webhooks/securing"}]}"

postSETUP: function (req, callback) {
   
        var twitter_oauth = {
            consumer_key: config.consumer_key,
            consumer_secret: config.consumer_secret,
            token: config.access_token,
            token_secret: config.access_token_secret
        }
       var WEBHOOK_URL = 'https://nodechatbotjson.azurewebsites.net/webhook/twitter'
       // request options
       var  request_options = {
            url: 'https://api.twitter.com/1.1/account_activity/webhooks.json',
            oauth: twitter_oauth,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            },
            form: {
                url: WEBHOOK_URL
            }
        }
        // POST request to send Direct Message
         request.post(request_options, function (error, response, body) {
            callback(null, body);
        })
},

#13

I wrote about using webhooks with NodeJS running on AWS lambda:
https://www.linkedin.com/pulse/handling-twitter-webhooks-aws-lambda-step-functions-frederik-willaert

It’s pretty extensive but should contain some answers for you.


#14

Excellent work. I have tryied with some of your ideas but i still get "{“errors”:[{“code”:214,“message”:"Webhook URL does not meet the requirements. Please consult: https://dev.twitter.com/webhooks/securing"}]}"

And I am stopped

Yours Pedro


#15

As you will see on that page…

Response requirements

  • Valid response_token and JSON format.
  • Latency less than 1 second.
  • 200 HTTP response code.

Have you verified that your webhook is fulfilling all of these? I was not able to test it as it does not appear to be at the URL you listed above.


#16

We have this response
{“fecha”:“Wed Jun 28 2017 12:26:21 GMT+0000 (Coordinated Universal Time)”,“nombre”:“prueba post en webhook/twitter”}
less than a second

Is it important the 200 HTTP? How do I build this message. Please

Yours Pedro


#17

You need to be responding with a CRC token as described in the docs and shown in the example project.


#18

I have less tha a second
200 http
a jason

app.post(’/webhook/twitter’, function (request, response) {
var fecha = new Date()
//var respuesta = { “fecha”: fecha.toString(), “nombre”: “prueba post en webhook/twitter” }
var respuesta = { “status”: 200, “data”: “Webhook URL ok” }

//var respuesta=request.body
response.end(200,JSON.stringify(respuesta));
//azurefunctions.postWEBHOOK(respuesta, function (err, flag) {
//   // response.end(JSON.stringify(respuesta));
//    response.end(respuesta);
//})

});

NOTHING still get
"{“errors”:[{“code”:214,“message”:"Webhook URL does not meet the requirements. Please consult: https://dev.twitter.com/webhooks/securing"}]}"


#19

You are not responding in the JSON response with a CRC token as documented. If the call response is not correct then this may be the result.

We are trying hard to help you, and the documentation and sample is there. We would appreciate it if you can look into this on your side.

I’ll also remind you that this particular API is in beta and brand new - so you should expect reference code to be limited, we don’t have lots of time to walk you through issues, and that functionality may change as we develop.

Thanks!


#20

Thanks. I am using the following documentation and there is no crc token?

Yours Pedro


#21

That’s only one part of the code. That’s the piece where you register the webhook callback. You also need a server listening, ready to respond when our side calls you to check for a valid set of criteria (response time and CRC token).