प्रशिक्षण
मॉड्यूल
Build conversational bots for Microsoft Teams - Training
Learn how to create bots to enable conversational interactions with users through text, interactive cards, and task modules in Microsoft Teams.
यह ब्राउज़र अब समर्थित नहीं है.
नवीनतम सुविधाओं, सुरक्षा अपडेट और तकनीकी सहायता का लाभ लेने के लिए Microsoft Edge में अपग्रेड करें.
महत्वपूर्ण
This article is based on the v3 Bot Framework SDK. If you are looking for current documentation version 4.6 or later of the SDK, see the conversational bots section.
A proactive message is a message that is sent by a bot to start a conversation. You may want your bot to start a conversation for many reasons, including:
Sending a message to start a new conversation thread is different than sending a message in response to an existing conversation. When your bot starts a new conversation, there's no pre-existing conversation to post the message to. To send a proactive message, you need to:
When creating proactive messages you must call MicrosoftAppCredentials.TrustServiceUrl
, and pass in the service URL before creating the ConnectorClient
used to send the message. If you don't, a 401: Unauthorized
response is received by your app. For more information, see the samples.
Sending proactive messages is an effective way to communicate with your users. However, from the user's perspective, the message appears unprompted. If there's a welcome message, it's the first time that they’ve interacted with your app. It's important to use this functionality and provide the complete information to the user to understand the purpose of this message.
Proactive messages generally fall into one of two categories, welcome messages or notifications.
When using proactive messaging to send a welcome message to a user, ensure that from the user's perspective, the message appears unprompted. If there's a welcome message, it's the first time that they’ve interacted with your app. The best welcome messages include:
When using proactive messaging to send notifications you need to make sure your users have a clear path to take common actions based on your notification, and a clear understanding of why the notification occurred. Good notification messages generally includes:
Bots can create new conversations with an individual Microsoft Teams user by obtaining the user's unique ID and tenant ID. You can obtain these values using one of the following methods:
conversationUpdate
event when your app is installed in a personal scope, or new members are added to a channel or group chat that.नोट
Proactively installing apps using graph is in beta.
Occasionally it may be necessary to proactively message users that haven't installed or interacted with your app previously. For example, you want to use the company communicator to send messages to your entire organization. For this scenario, you can use the Graph API to proactively install your app for your users, then cache the necessary values from the conversationUpdate
event your app will receive upon install.
You can only install apps that are in your organizational app catalog, or the Microsoft Teams Store.
See Install apps for users in the Graph documentation for complete details. There's also a sample in .NET.
Be sure that you authenticate and have a bearer token before creating a new conversation using the REST API.
POST {Service URL of your bot}/v3/conversations
{
"bot": {
"id": "c38eda0f-e780-49ae-86f0-afb644203cf8",
"name": "The Bot"
},
"members": [
{
"id": "29:012d20j1cjo20211"
}
],
"channelData": {
"tenant": {
"id": "197231joe-1209j01821-012kdjoj"
}
}
}
Provide id
as your bot app ID and name
as your bot name. You can get the members
id
from your bots TurnContext
object such as turnContext.Activity.From.Id
. Similarly, id
of tenant, from your bots TurnContext
object such as turnContext.Activity.ChannelData.Tenant.Id
.
You must supply the user ID and the tenant ID. If the call succeeds, the API returns with the following response object.
{
"id":"a:1qhNLqpUtmuI6U35gzjsJn7uRnCkW8NiZALHfN8AMxdbprS1uta2aT-jytfIlsZR3UZeg3TsIONNInBHsdjzj3PtfHuhkxxvS1jZZ61UAbw8fIdXcNSJyTJm7YvHFOgxo"
}
This ID is the personal chat's unique conversation ID. Store this value and reuse it for future interactions with the user.
This example uses the Microsoft.Bot.Connector.Teams NuGet package.
// Create or get existing chat conversation with user
var response = client.Conversations.CreateOrGetDirectConversation(activity.Recipient, activity.From, activity.GetTenantId());
// Construct the message to post to conversation
Activity newActivity = new Activity()
{
Text = "Hello",
Type = ActivityTypes.Message,
Conversation = new ConversationAccount
{
Id = response.Id
},
};
// Post the message to chat conversation with user
await client.Conversations.SendToConversationAsync(newActivity, response.Id);
var address =
{
channelId: 'msteams',
user: { id: userId },
channelData: {
tenant: {
id: tenantId
}
},
bot:
{
id: appId,
name: appName
},
serviceUrl: session.message.address.serviceUrl,
useAuth: true
}
var msg = new builder.Message().address(address);
msg.text('Hello, this is a notification');
bot.send(msg);
Your team-added bot can post into a channel to create a new reply chain. If you're using the Node.js Teams SDK, use startReplyChain()
, which gives you a fully populated address with the correct activity ID and conversation ID. If you're using C#, see the following example.
Alternatively, you can use the REST API and issue a POST request to /conversations
resource.
The .NET example is from this sample
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Teams.Models;
using Microsoft.Teams.TemplateBotCSharp.Properties;
using System;
using System.Threading.Tasks;
namespace Microsoft.Teams.TemplateBotCSharp.Dialogs
{
[Serializable]
public class ProactiveMsgTo1to1Dialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var channelData = context.Activity.GetChannelData<TeamsChannelData>();
var message = Activity.CreateMessageActivity();
message.Text = "Hello World";
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new TeamsChannelData
{
Channel = new ChannelInfo(channelData.Channel.Id),
},
Activity = (Activity) message
};
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.MaxValue);
var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
context.Done<object>(null);
}
}
}
Platform Docs प्रतिक्रिया
Platform Docs एक ओपन सोर्स प्रोजेक्ट है. प्रतिक्रिया प्रदान करने के लिए लिंक का चयन करें:
प्रशिक्षण
मॉड्यूल
Build conversational bots for Microsoft Teams - Training
Learn how to create bots to enable conversational interactions with users through text, interactive cards, and task modules in Microsoft Teams.
दस्तावेज़ीकरण
Send and receive messages with a bot - Teams
In this module, learn to have a conversation with a Microsoft Teams bot, Proactive messages, Conversation basics, Message content and formatting
Create Bot to Send Proactive Messages - Teams
In this module, learn how to send proactive messages, such as welcome messages, scheduled messages, and notifications from a bot.
In this module, learn how to handle events in bots for Microsoft Teams, Teams member or bot addition, Team member or bot removed and more.