Welcome to the Weather-Chatbot for Wizyroom
Weather-Chatbot is a Node.js chatbot for Wizyroom. In this example we propose to use Ngrok: a secure introspectable tunnels to your localhost, to test your chatbot locally without deploying it.
Requirement
Installation
The application has very minimal dependencies and requirements aside from a typical Node.js stack.
- Simply clone this repository.
- Run the prompt command.
- Install the dependencies via
npm install
under your project root file. - Run the chatbot locally via
npm start
it will run on localhost:8080. - Download Ngrok and extract file content in project path.
- Run the commande prompt and run server via
ngrok http 8080
.
Development
You can retrieve all documentation for Wizyroom API:
Learn how to use it and feel free to leave comment.
- Create a get request to test the chatbot running
app.get('/', function(req, res, next) {
res.render('index.ejs');
});
- Get the body of your request when using the Node.js framework Express
req.body
contain the posted data.
app.post('/weather', function(req, res){
operation.getWeather(req, res)
})
-
Get your own APIXU API key from here and replace it in the code above in
var apiKey
. -
This simple application will return weather for country specified by the user everytime the chatbot is triggered in Wizyroom
function getWeather(req, res){
var data = req.body;
var message = data.message;
var bot = data.bot;
var token = data.token;
var apiKey = 'cacdf29dc2be47d484a105606152306'; //Weather API key change it with yours
var is_reply = true;
var country = message.body.replace(bot.mention_text , ""); //Get the country name from message recieved
var path = 'http://api.apixu.com/v1/current.json?key=' + apiKey + '&q=' + country.trim();
//Send request to apixu API and handel response to send result back to Wizyroom
request.get(
path,
{
json: {},
headers: {}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
var replyMsg = "Weather for "+country.trim()+" is: \n"+
" • Temp(°C) = " + body.current.temp_c + "°\n"+
" • Condition = " + body.current.condition.text + "\n"+
" • Humidity = " + body.current.humidity +"%"
res.status(200).send({'body': replyMsg, 'is_reply':is_reply});
}else{
console.log("error")
res.status(500).send({'body': "server error, conversation not updated", 'status': "error"});
}
}
);
}
The chatbot reply depends on the country specified by the user to the bot. The chatted country is sent to APIXU then APIXU response is formated and sent back to Wizyroom user.
Deployment
In order to be able to deploy your bot we propose to use Heroku.com.
- First of all Fork this project to your account
- Log in or Sign up to Heroku.
- Go to dashboard.
- Click new and create a new app.
- Specify an App Name then Create app.
- Use GitHub for Deployment method.
- Connect GitHub to Heroku and authorize application.
- Now search for our project WizyRoom-Sample-Bot and connect it.
- Click on Deploy Branch Master.
- Once deployment done you can view your app by clicking on Open app example
WizyRoom integration
Time to use your bot in Wizyroom and test it.
- Go to your Wizyroom account admin panel
- Add chatbot
- Specify all required fields (Server URL is the url of the deployed application on Heroku with /weather at the end of URLs) example : https://wizyroom-weather-bot.herokuapp.com/weather
- Go to a room and invite your bot
- Mention your created bot and start a conversation
License
The code provided in this sample is licensed under the Wizy.io License.