Reputation: 868
I am trying to send a message in a channel using the bot but getting the below error.
error: TypeError: Cannot read property 'create' of undefined
on debugging I found that context.adapter.ConnectorFactoryKey
is null
I am taking the motivation of the code from the source here:
teamsBot.js
const {
TurnContext,
MessageFactory,
TeamsActivityHandler,
teamsGetChannelId
} = require('botbuilder');
class TeamsStartNewThreadInChannel extends TeamsActivityHandler {
constructor() {
super();
this.onMessage(async (context, next) => {
const teamsChannelId = teamsGetChannelId(context.activity);
const message = MessageFactory.text('This will be the first message in a new thread');
const newConversation = await this.teamsCreateConversation(context, teamsChannelId, message);
await context.adapter.continueConversationAsync(process.env.MicrosoftAppId, newConversation[0], async turnContext => {
await turnContext.sendActivity(MessageFactory.text('This will be the first response to the new thread'));
});
await next();
});
}
async teamsCreateConversation(context, teamsChannelId, message) {
const conversationParameters = {
isGroup: true,
channelData: {
channel: {
id: teamsChannelId
}
},
activity: message
};
const connectorFactory = context.turnState.get(context.adapter.ConnectorFactoryKey);
const connectorClient = await connectorFactory.create(context.activity.serviceUrl);
const conversationResourceResponse = await connectorClient.conversations.createConversation(conversationParameters);
const conversationReference = TurnContext.getConversationReference(context.activity);
conversationReference.conversation.id = conversationResourceResponse.id;
return [conversationReference, conversationResourceResponse.activityId];
}
}
Manifest.json
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.11/MicrosoftTeams.schema.json",
"manifestVersion": "1.11",
"version": "1.0.0",
"id": "ef2axxx-xx-xx-xxx-4810d",
"packageName": "com.microsoft.teams.extension",
"developer": {
"name": "Teams App, Inc.",
"websiteUrl": "https://www.example.com",
"privacyUrl": "https://www.example.com/index.html#/privacy",
"termsOfUseUrl": "https://www.example.com/index.html#/termsofuse"
},
"icons": {
"color": "resources/color.png",
"outline": "resources/outline.png"
},
"name": {
"short": "heloo-local-debug",
"full": "heloo-local-debug"
},
"description": {
"short": "Short description of heloo-local-debug",
"full": "Full description of heloo-local-debug"
},
"accentColor": "#FFFFFF",
"bots": [
{
"botId": "ddd5e0xxx-xxx-xxx-xxxx-5e89c1a83d",
"scopes": [
"personal",
"team",
"groupchat"
],
"supportsFiles": false,
"isNotificationOnly": false,
"commandLists": [
{
"scopes": [
"personal",
"team",
"groupchat"
],
"commands": [
{
"title": "welcome",
"description": "Resend welcome card of this Bot"
},
{
"title": "learn",
"description": "Learn about Adaptive Card and Bot Command"
}
]
}
]
}
],
"composeExtensions": [],
"configurableTabs": [],
"staticTabs": [],
"permissions": [
"identity",
"messageTeamMembers"
],
"validDomains": [
"d510-xxx-xxx-215.ngrok.io"
],
"webApplicationInfo": {
"id": "6-xxx-xxx-xxx7",
"resource": "api://botid-dd-xxxxx-xxx-xxx565e89c1a83d"
}
}
What I am doing wrong above?
Upvotes: 0
Views: 431
Reputation: 10854
Remember that Bot Framework can be used in many contexts, Teams is just one of those. However, when you're in Teams, there is no concept of "creating" a conversation with the user. There is only a single "conversation" ever, and you are basically "continuing" the conversation. As a result, you want to call continueConversation
. Have a look at this sample I posted a while back - I've linked right to the most relevant line.
Please note, as I mentioned above, this requires the user to have installed your bot app already - you can't start a proactive conversation with a user if there is no existing context. If you want to learn how you can pre-install the bot, see this question: Proactively Install / Push Apps in Teams for Multiple Users.
Upvotes: 1