Search code examples
node.jsazurebotframeworkdirect-line-botframeworkmake.com

Sending a Webhook Response to a Conversation using session.send in Azure Bot Framework (Nodejs)


Currently attempting to integrate Azure Bot Framework with external platforms like Integromat. I am using the code snippet below to send the first name and mobile number submitted by the user. The user input is sent to an integromat webhook, which sends a webhook response.

bot.dialog('WebTest', function (session) {
    session.send('conversation.id: ' + session.message.address.conversation.id);
    session.userData.convoID = session.message.address.conversation.id;
   // var request = require('request');         
   // var url = "https://hook.integromat.com/y6d18ahnsfanbkwqfdmygkwd2ft93vr2"
        request.post({   
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
          url: 'https://hook.integromat.com/ynwbud77o7up7rrhl3m8tvdriquhtess',
                body: 'first=' + session.userData.first + '&mobile=' + session.userData.mobile + '&convoID=' +session.userData.convoID
        }).on('response', function (response) {
            //session.send(response);
            response.on('data', function (data) {
                console.log('data: ' + data);
            })
           // session.send(data)
        });    
   // session.send(data);
   //session.send(response);
    session.send("This service is still under construction");
}).triggerAction({ matches: /^webby/i })

The response is correctly logged in the console https://i.sstatic.net/XQC8u.png

However, I am not sure as how I would be able to send it back to the bot and display it to the user.

I have explored the Directline API as an option, acquiring the conversation ID and following the documentation. I used this link: https://directline.botframework.com/v3/directline/conversations/{{1.convoID}}/activities And sent the following request content as a json payload along with the authorization key as a header:

{
    "type": "message",
    "from": {
        "id": "user1"
    },
    "text": "hello"
}

Although for this, I am getting a 404 error, with a "BadArgument" and "Unknown conversation" error.

Any help that would put me in the right direction would be appreciated, thanks!


Solution

  • I am not 100 percent sure what specifically you would like to do with your data within this function:

    response.on('data', function (data) {
        console.log('data: ' + data);
    })
    

    But the reason that session.send(data) doesn't work after this should be due to data being an object rather than a string. In this case you can do anything to make the argument a string instead, from something as simple as JSON.stringify(data) to formatting an output with the fields.

    If a directline call was made from inside the bot, it likely fired off without failure because you can communicate from bot to bot via that API; but if the conversationID resolves to one within the bot currently being used (or simply does not exist yet) then an error is expected.