0
votes

I'm sending message to my bot using Microsoft BotConnector but they are not being logged as normal messages. For logging messages to the DB I wrote custom logger :

class CustomLogger {
    /**
     * Log an activity to the transcript file.
     * @param activity Activity being logged.
     */
    constructor() {
        this.conversations = {};
    }

    logActivity(activity) {

        if (activity) {
            console.log("Log information")
        }


        if (!activity) {
            throw new Error("Activity is required.");
        }
        if (activity.conversation) {
            var id = activity.conversation.id;
            if (id.indexOf("|" !== -1)) {
                id = activity.conversation.id.replace(/\|.*/, "");
            }
        }

        if (activity.type === "message") {
            Conv.create({
                text: activity.text,
                conv_id: activity.conversation.id,
                from_type: activity.from.role,
                message_id: activity.id || activity.replyToId
            }).then(() => {
                console.log("logged");
            });
            delete this.conversations[id];
        }
    }
}

it works great with normal messages but it is no working with the messages that are sent to

POST /v3/conversations/{conversationId}/activities

via microsoft bot connector.

When I send message using the the bot connector it doesn't log the request via activity.

Code that I'm using to send proactive msg:

/**
 * Send message to the user.
 */
function sendMessage(token, conversation, name) {

  var config = {
    headers: { "Authorization": "Bearer " + token }
  };

  var bodyParameters = {
    "type": "message",
    "text": name
  }

  axios.post(
    'https://smba.trafficmanager.net/apis/v3/conversations/29:XXXXXXXXXXXXXXX/activities',
    bodyParameters,
    config
  ).then((response) => {
    console.log(response)
  }).catch((error) => {
    console.log(error)
  });
}


let name = "Hey, How was your week?";
let conversation = "29:XXXXXXXXXXXXXXX";

run(conversation, name);
1
Can you add the code for how you are sending the proactive messages? I don't believe proactive messages flow through the same middleware stream as regular messages, so, in theory, it shouldn't hit your transcript logger. Likely, you probably have to manually log it when you send the activity. - tdurnford
I updated the code that I'm using to send the proactive message. Yes, it appears that it doesn't go through the middleware. Okay so even if I would log it to DB using some code while sending it, how can I intercept the response to that message? (my approach is to ask proactive feedback question and collect the response) I would need to intercept the message sent by the proactive notification and then log the follow up answer provided by the user to our database. For instance, the bot could say "How was your week?" and then the answer provided should be logged in our database. - Kirol54
And you're sending the proactive message from an Azure Function correct? - tdurnford

1 Answers

0
votes

Instead of using the REST API to send proactive messages to users, I would recommend using the BotFramework Adapter to continue the conversation with the user. When you send the proactive message from the adapter, the activity passes through the logger middleware and gets saved to storage. If you would like to initiate the proactive message from an Azure Function, you can set up another messaging endpoint in the index file that you call from the function. Take a look at the code snippets below.

index.js

// Listen for incoming notifications and send proactive messages to user.
server.get('/api/notify/:conversationID', async (req, res) => {
    const { conversationID } = req.params;
    const conversationReference = conversationReferences[conversationID];

    await adapter.continueConversation(conversationReference, async turnContext => {
        await turnContext.sendActivity('proactive hello');
    });


    res.setHeader('Content-Type', 'text/html');
    res.writeHead(200);
    res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
    res.end();
});

For more details I would take a look at this Proactive Messages Sample. It is in the samples-work-in-progress branch and might change slightly, but it is a great example of how to configure your project to send a proactive message from a Restify endpoint.

Hope this helps!