Using webhook in workplace for azure chatbots - Python for Data Analytics

Python Programs | Python Tricks | Solution for problems | Data Cleaning | Data Science

Using webhook in workplace for azure chatbots

Requirements:

  • ngrok application
  • azure account
  • workplace account and admin access
  • created chatbot from azure

Download ngrok from the link and start the application it will open a cmd prompt enter the command
ngrok http 3798
Port number could be anything, then hit enter it will display some information like
From there copy the url that start with https login to your azure account then
select your chatbot go to settings and place it in Messaging endpoint as in
following picture
Where /webhook is endpoint where we will get response from workplace.
Now login into workplace account —> Integrations —> select the page or group
 —> scroll down until you see configure webhook headlines.
Enter ngrok url under callback url that you copied from ngrok application
as in here it will be : https://952dc55c.ngrok.io
Under verify token enter a string of your choice, as we are using
“myverifytoken” string here.
Select the services for which you want to get notify, we are selecting
here – posts and comments.
Now you need to place the code below inside your index.js file of chatbot
server.get('/',(req,res)=>{

if (req.query['hub.mode'] === 'subscribe' &&

req.query['hub.verify_token'] === "myverifytoken") {

console.log('Validating webhook');

res.status(200).send(req.query['hub.challenge']);

} else {

console.error('Failed validation. Make sure the validation tokens match.');

res.sendStatus(403);

}

});
Above code will verify the first request coming from workplace and then allow
our chatbot to receive all other updates from group.
Below code will listen all the requests send from workplace and this the place
where you can extract data are post back like in our case we are taking the post
message or comment message and posting it back in same group:

server.post('/webhook', (req, res) => {
let data = req.body.value.message;
console.log(req.body);
try{
console.log("DATA=>"+data);
request.post("https://graph.facebook.com/"+group_id+"/feed?message="+data+"&access_token="+access_token)
}catch(err){
console.log("Failed to post in group");
}
fs.writeFileSync("temp.txt",req.body,(error)=>{
if (err) console.log(err);
console.log("Successfully Written to File.");
});

res.status(200);
res.send();
// Route received a request to adapter for processing

});

3 comments:

  1. Sdsuv University BCOM 1st Year Result 2020
    Sdsuv University BCOM 2nd Year Result 2020
    Sdsuv University BCOM 3rd Year Result 2020
    You’d outstanding guidelines there. I did a search about the field and identified that very likely the majority will agree with your web page.

    ReplyDelete
  2. Thank you for sharing the article. The data that you provided in the blog is informative and effective.

    Servicenow Training in Hyderabad

    ReplyDelete