This code is modified version of Line Messenger NodeJS SDK. You can use it to test your bot locally for your side project. Please use it wisely, you should use the official SDK for more serious cases. For more serious cases, please follow the official guideline on how to test your bot locally here : line/line-bot-sdk-nodejs#32 This repo will not be maintained.
👍 You can test your bot locally, you can test directly on your own computer without passing through long-boring build pipeline, No need to put on staging server, Heroku / Azure or other PaaS provider just to test your bot is working. 🖥️
With this pull request, the developers are able to test their bots on local computer/environment. Accelerating bot development by bypassing x-line-signature
verification.
You can write a simple Line API Mock Server like this :
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.get('/', function(req, res){
res.send('Hello world');
});
app.post('/message/reply',function(req, res){
console.dir(req.body);
res.json(req.body);
});
app.post('/message/push', function(req, res){
console.dir(req.body);
res.json(req.body);
});
app.get('/profile/:userId', function(req, res){
var users = {
'member_1':{
'displayName': 'Spongebob',
'userId': 'member_1',
'pictureUrl': 'http://placeholder.it/256x256',
'statusMessage': 'exampleStatusMessage',
},
'member_2':{
'displayName': 'Patrick Star',
'userId': 'member_2',
'pictureUrl': 'http://placeholder.it/256x256',
'statusMessage': 'exampleStatusMessage',
},
};
var selectedUser = users[req.params.userId];
console.log("Returning profile for userId = "+req.params.userId);
console.dir(selectedUser);
res.json(selectedUser);
});
app.post('*', function(req, res){
showTime();
console.dir(req.body);
res.json(req.body);
});
app.listen(8000, function(){
console.log("listening on port 8000");
});
then run your Line API Mock Server with :
node LineMockserver.js
In this case, we are listening to port 8000, then run your bot server using this command :
API_BASE_URL=http://localhost:8000/ TEST=true node server.js
You can use any REST client to create a Http request to your bot server. Make sure you follow the Line API payload structure as written on their docs while making your request.
In this case, I use Postman Chrome Plugin since my bot server running on port 3000
, I put localhost:3000
on Postman's address bar, set the Content-Type
header to application/json
and put Line Message Object, in the body. like this :
{
"events": [
{
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "message",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "member_1"
},
"message": {
"id": "325708",
"type": "text",
"text": "help"
}
}
]
}