-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// An example to show use of webhooks. | ||
|
||
var contextMap = require('bot-context'); | ||
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var request = require('request'); | ||
|
||
var app = express(); // Set up an express webservice. You can use restify, Koa etc. | ||
|
||
app.use(bodyParser.json()); | ||
|
||
// Incoming webhook. | ||
app.post('/recieve', (req, res) => { | ||
let ctx = contextMap.getOrCreate(req.body.userId); // Assuming userId is in the json payload. | ||
if (!ctx.isSet()) { | ||
init(userId); // initialize the actions. | ||
} | ||
|
||
ctx.match(message, function (err, match, contextCb) { | ||
if (!err) contextCb(userId, match); | ||
}); | ||
}); | ||
|
||
function init(userId) { | ||
let ctx = contextMap.getOrCreate(userId); | ||
ctx.set( | ||
/.*/, // The base matcher to match anything. | ||
(match) => doSomethingAndRespond(userId)); | ||
} | ||
|
||
function doSomethingAndRespond(userId) { | ||
ctx.set( | ||
// Set some more context here. | ||
); | ||
outgoingWebhook(userID, "bla bla"); | ||
} | ||
|
||
function outgoingWebhook(userId, message) { | ||
request.post({ | ||
url: 'https://<webhook-endpoint-here>', | ||
form: { | ||
userId: userId, | ||
message: message | ||
} | ||
}, (err, resp, body) => { | ||
// Handle webhook err/response. | ||
}); | ||
} |