infodev
infodev

Reputation: 5235

Bot Framework v4 - initiate chat with a user ( proactive messages )

I would like to start a conversation with a user without that he already chatted with the bot.

I'm proceeding bu getting the conversationId when user install the bot app as mentionned here

Here how I catch reference and serviceUrl by using processActivity event

Then I use continueConversation + reference

const {
    BotFrameworkAdapter,
} = require('botbuilder');
const adapter = new BotFrameworkAdapter({
    appId: "xxxx",
    appPassword: "xxxxy"
})
module.exports = async function(context, req) {
    console.log(adapter);
    try {

        adapter.processActivity(req, context.res, async (turnContext) => {
            const reference = turnContext.activity.conversation.id
            const serviceUrl = turnContext.activity.serviceUrl

            await adapter.continueConversation(reference, async (turnContext) => {
                try {
                    await turnContext.sendActivity("this is proacive message");

                } catch (e) {
                    console.log(e)
                }
            });

        });
    } catch (e) {
        console.log(e)
    }
    return;
};

I'm getting this error

Error: BotFrameworkAdapter.sendActivity(): missing serviceUrl.

I have checked the turnContext.activity values. I get : {"type":"event","name":"continueConversation"}" all other values are undefined ( serviceUrl also )

I noticed that turnContext.activityinside adapter.processActivity is not the same as in adapter.continueConversation and have all the serviceUrl and conversationId

How can I edit this code example to be able to send proactive messages to users?

Upvotes: 0

Views: 698

Answers (1)

JJeff
JJeff

Reputation: 121

The const reference needs to be ConversationReference not just the Id if you are using it for BotFrameworkAdapter.continueConversation().

A ConversationReference can be retrieved by using:

const reference = TurnContext.getConversationReference(turnContext.activity);

https://learn.microsoft.com/en-us/javascript/api/botbuilder/botframeworkadapter?view=botbuilder-ts-latest#continueconversation-partial-conversationreference----context--turncontext-----promise-void--

https://learn.microsoft.com/en-us/javascript/api/botbuilder-core/turncontext?view=botbuilder-ts-latest#getconversationreference-partial-activity--

Upvotes: 2

Related Questions