Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ app.use(express.urlencoded({ extended: true }));

const port = 3000;

const myArgs = process.argv.slice(2);

app.all('/validate', function (req, res) {
console.log("-------------- Validate Request --------------");
res.json( {"confirmation_code":myArgs[0]} );
})

app.all('/*', function (req, res) {
console.log("-------------- New Request --------------");
console.log("Headers:"+ JSON.stringify(req.headers, null, 3));
Expand Down
42 changes: 42 additions & 0 deletions appTwitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var express = require('express');
crypto = require('crypto')

var app = express();
app.use(express.json());
const port = 3000;
const consumer_secret = "Twitter API Key Secret";

get_challenge_response = function(crc_token, consumer_secret) {
hmac = crypto.createHmac('sha256', consumer_secret).update(crc_token).digest('base64')
return hmac
}

app.post('/*', function (req, res) {
console.log("-------------- New Request POST --------------");
console.log("Headers:"+ JSON.stringify(req.headers, null, 3));
console.log("Body:"+ JSON.stringify(req.body, null, 3));
res.json({ message: "Thank you for the message" });
})

// Add support for GET requests to Twitter webhook
app.get("/*", (req, res) => {
// Parse the query param
var crc_token = req.query["crc_token"];
console.log("-------------- New Request GET --------------");
console.log("Headers:"+ JSON.stringify(req.headers, null, 3));
console.log("Body:"+ JSON.stringify(req.body, null, 3));
// Check if a token is in the query string of the request
if (crc_token) {
var hash = get_challenge_response(crc_token, consumer_secret)
console.log("crc token hash="+ hash);
res.status(200);
res.send({response_token: 'sha256=' + hash})
} else {
res.status(400);
res.send({ message: "Error: crc_token missing from request."})
}
});

app.listen(port, function () {
console.log(`Example Twitter app listening at ${port}`)
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"start": "node app.js",
"startSlack": "node appSlack.js",
"startFacebook": "node appFB.js",
"startDropbox": "node appDropbox.js"
"startDropbox": "node appDropbox.js",
"startTwitter": "node appTwitter.js"
},
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions testhmac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const crypto = require('crypto');

const verifySignature = function(secret, payload){
const hash = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('base64');
return `sha256=${hash}`;
}

payload={"event_id":"01GJ0FH2Y3H1TYFF8EP9H2RHSG","event_type":"form_response","form_response":{"form_id":"uyifVtg4","token":"a5f3s9fabveqqec7o9l9sa5f3s9fitvt","landed_at":"2022-11-16T15:04:08Z","submitted_at":"2022-11-16T15:04:43Z","definition":{"id":"uyifVtg4","title":"My typeform","fields":[{"id":"HTE1PyMQrlQK","ref":"01GJ0DJYHF6NH3B6NH0YSP1DVF","type":"short_text","title":"Hello, what's your name?","properties":{}},{"id":"7EfljHzxs76Q","ref":"01GJ0DJYHSJ8HMEG2MQ1E819TN","type":"multiple_choice","title":"Nice to meet you, {{field:01GJ0DJYHF6NH3B6NH0YSP1DVF}}, how is your day going?","properties":{},"choices":[{"id":"rbTzd6XVsx8x","label":"Terrific!"},{"id":"tHnbelIABoFL","label":"Not so well..."}]}]},"answers":[{"type":"text","text":"Felippe","field":{"id":"HTE1PyMQrlQK","type":"short_text","ref":"01GJ0DJYHF6NH3B6NH0YSP1DVF"}},{"type":"choice","choice":{"label":"Terrific!"},"field":{"id":"7EfljHzxs76Q","type":"multiple_choice","ref":"01GJ0DJYHSJ8HMEG2MQ1E819TN"}}]}}

secret = "12345";
hmac = verifySignature(secret, JSON.stringify(payload));
console.log(hmac);