Skip to content

Commit 3ce5d70

Browse files
authored
Merge pull request #289607 from palatter/palatter/update-bot-quickstart
Update Bot Chat Quickstart
2 parents 804d82d + b056eef commit 3ce5d70

File tree

2 files changed

+39
-146
lines changed

2 files changed

+39
-146
lines changed
-51.4 KB
Loading

articles/communication-services/quickstarts/chat/quickstart-botframework-integration.md

Lines changed: 39 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,42 @@ In this quickstart, you learn how to:
2828

2929
- An Azure account and an active subscription. Create an [account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3030
- [Visual Studio 2019 or later](https://visualstudio.microsoft.com/vs/).
31-
- The latest version of .NET Core. In this quickstart, we use [.NET Core 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1). Be sure to install the version that corresponds with your instance of Visual Studio, 32-bit or 64-bit.
31+
- The latest version of .NET Core. (https://dotnet.microsoft.com/download/dotnet/).
3232
- Bot framework [SDK](https://github.com/microsoft/botframework-sdk/#readme)
3333

3434
## Create and deploy a bot in Azure
3535

3636
To use Azure Communication Services chat as a channel in Azure Bot Service, first deploy a bot. To deploy a bot, you complete these steps:
3737

38-
- Create an Azure Bot Service resource
39-
- Get the bot's app ID and password
40-
- Create a web app to hold the bot logic
41-
- Create a messaging endpoint for the bot
38+
- [Create an Azure Bot Service resource](#create-an-azure-bot-service-resource)
39+
- [Get the bot's app ID and password](#get-the-bots-app-id-and-app-password)
40+
- [Create a web app to hold the bot app](#create-a-web-app-to-hold-the-bot-app)
41+
- [Create a messaging endpoint for the bot](#create-a-messaging-endpoint-for-the-bot)
4242

4343
### Create an Azure Bot Service resource
4444

45-
First, [use the Azure portal to create an Azure Bot Service resource](/azure/bot-service/abs-quickstart?tabs=userassigned). Communication Services Chat channel supports single-tenant bots, managed identity bots, and multi-tenant bots. For the purposes of this quickstart we will use a *multi-tenant* bot.
45+
First, [use the Azure portal to create an Azure Bot Service resource](/azure/bot-service/abs-quickstart?tabs=userassigned). Communication Services Chat channel supports single-tenant bots, managed identity bots, and multitenant bots.
4646

47-
To set up a single-tenant or managed identity bot, review [Bot identity information](/azure/bot-service/bot-builder-authentication?tabs=userassigned%2Caadv2%2Ccsharp#bot-identity-information).
48-
49-
For a managed identity bot, you might have to [update the bot service identity](/azure/bot-service/bot-builder-authentication?tabs=userassigned%2Caadv2%2Ccsharp#to-update-your-app-service).
47+
- For the purposes of this quickstart we will use a `multitenant` bot.
48+
- To set up a `single-tenant` or `managed identity` bot, review [Bot identity information](/azure/bot-service/bot-builder-authentication?tabs=userassigned%2Caadv2%2Ccsharp#bot-identity-information).
49+
- For a `managed identity` bot, you might have to [update the bot service identity](/azure/bot-service/bot-builder-authentication?tabs=userassigned%2Caadv2%2Ccsharp#to-update-your-app-service).
5050

5151
### Get the bot's app ID and app password
5252

5353
Next, [get the Microsoft app ID and password](/azure/bot-service/abs-quickstart?tabs=userassigned#to-get-your-app-or-tenant-id) that are assigned to your bot when it's deployed. You use these values for later configurations.
5454

55-
### Create a web app to hold the bot logic
55+
### Create a bot app and publish it to a web app
5656

57-
To create a web app for your bot, you can revise [Bot Builder samples](https://github.com/Microsoft/BotBuilder-Samples) for your scenario or use the [Bot Builder SDK](/composer/introduction) to create a web app. One of the simplest samples is [Echo Bot](https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/02.echo-bot).
57+
To create a bot you can do one of the following:
5858

59-
Azure Bot Service typically expects the Bot Application Web App Controller to expose an endpoint in the form `/api/messages`. The endpoint handles all messages that are sent to the bot.
59+
- Revise [Bot Builder samples](https://github.com/Microsoft/BotBuilder-Samples) for your scenario, create a web app, and then deploy your bot sample to it.
60+
- Use the [Bot Builder SDK](/composer/introduction) to create and publish a bot to a web app.
61+
62+
For this quickstart we will use the [Echo Bot](https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/02.echo-bot) sample from the Bot Builder samples.
6063

61-
To create the bot app, either use the Azure CLI to [create an Azure App Service resource](/azure/bot-service/provision-app-service?tabs=singletenant%2Cexistingplan) or create the app in the Azure portal.
64+
#### Create a web app to hold the bot app
65+
66+
To create the web app, either use the Azure CLI to [create an Azure App Service resource](/azure/bot-service/provision-app-service?tabs=singletenant%2Cexistingplan) or create the app in the Azure portal.
6267

6368
To create a bot web app by using the Azure portal:
6469

@@ -76,7 +81,9 @@ To create a bot web app by using the Azure portal:
7681

7782
:::image type="content" source="./media/web-app-endpoint.png" alt-text="Screenshot that shows how to copy the web app endpoint URL.":::
7883

79-
### Create a messaging endpoint for the bot
84+
#### Create a messaging endpoint for the bot
85+
86+
Azure Bot Service typically expects the Bot Application Web App Controller to expose an endpoint in the form `/api/messages`. The endpoint handles all messages that are sent to the bot.
8087

8188
Next, in the bot resource, create a web app messaging endpoint:
8289

@@ -88,9 +95,9 @@ Next, in the bot resource, create a web app messaging endpoint:
8895

8996
:::image type="content" source="./media/smaller-bot-configure-with-endpoint.png" alt-text="Screenshot that shows how to create a bot messaging endpoint by using the web app hostname." lightbox="./media/bot-configure-with-endpoint.png":::
9097

91-
### Deploy the web app
98+
#### Deploy the web app
9299

93-
The final step to create a bot is to deploy the web app. For this quickstart, use the Echo Bot sample. The Echo Bot functionality is limited to echoing the user input. Here's how you deploy it to your web app in Azure:
100+
The final step to create a bot is to deploy the web app. For this quickstart, use the [Echo Bot](https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/02.echo-bot) sample. The Echo Bot functionality is limited to echoing the user input. Here's how you deploy it to your web app in Azure:
94101

95102
1. Use Git to clone this GitHub repository:
96103

@@ -105,12 +112,14 @@ The final step to create a bot is to deploy the web app. For this quickstart, us
105112

106113
```json
107114
{
115+
"MicrosoftAppType": "",
108116
"MicrosoftAppId": "<App-registration-ID>",
109-
"MicrosoftAppPassword": "<App-password>"
117+
"MicrosoftAppPassword": "<App-password>",
118+
"MicrosoftAppTenantId": ""
110119
}
111120
```
112121

113-
Next, use Visual Studio for C# bots to deploy the bot.
122+
Next, use Visual Studio or VS Code for C# bots to deploy the bot.
114123

115124
You also can use a Command Prompt window to [deploy an Azure bot](/azure/bot-service/provision-and-publish-a-bot?tabs=userassigned%2Ccsharp).
116125

@@ -162,142 +171,30 @@ When you have a Communication Services resource, you can set up a Communication
162171

163172
Now that you have the bot's Communication Services ID, you can create a chat thread with the bot as a participant.
164173

165-
### Create a new C# application
174+
### Follow the 'Add Chat to your app' quickstart
166175

167-
1. Run the following command to create a C# application:
176+
Follow the steps in the [Add Chat to your app](/azure/communication-services/quickstarts/chat/get-started?pivots=programming-language-csharp) quickstart to create a chat app.
168177

169-
```console
170-
dotnet new console -o ChatQuickstart
171-
```
178+
1. Replace <Resource_Endpoint> with the Communication Services endpoint from the [Get a Communication Service Resource](#get-a-communication-services-resource) step.
179+
1. Replace <Access_Token> with the user access token from the [Get a Communication Service Resource](#get-a-communication-services-resource) step.
180+
1. Replace <Access_ID> with the bots ACS_ID from the [Enable the Communication Services Chat channel](#enable-the-communication-services-chat-channel) step.
172181

173-
1. Change your directory to the new app folder and use the `dotnet build` command to compile your application:
182+
### Run the C# chat application locally
174183

175-
```console
176-
cd ChatQuickstart
177-
dotnet build
178-
```
179-
180-
### Install the package
181-
182-
Install the Communication Services Chat SDK for .NET:
184+
To run the chat application locally, use the `dotnet run` command:
183185

184186
```powershell
185-
dotnet add package Azure.Communication.Chat
186-
```
187-
188-
### Create a chat client
189-
190-
To create a chat client, use your Communication Services endpoint and the user access token you generated earlier. Use the `CommunicationIdentityClient` class from the Identity SDK to create a user and issue a token to pass to your chat client. Access tokens can be generated in the portal using the following [instructions](/azure/communication-services/quickstarts/identity/access-tokens).
191-
192-
Copy the following code and paste it in the *Program.cs* source file:
193-
194-
```csharp
195-
using Azure;
196-
using Azure.Communication;
197-
using Azure.Communication.Chat;
198-
using System;
199-
200-
namespace ChatQuickstart
201-
{
202-
class Program
203-
{
204-
static async System.Threading.Tasks.Task Main(string[] args)
205-
{
206-
// Your unique Communication Services endpoint
207-
Uri endpoint = new Uri("https://<RESOURCE_NAME>.communication.azure.com");
208-
209-
CommunicationTokenCredential communicationTokenCredential = new CommunicationTokenCredential(<Access_Token>);
210-
ChatClient chatClient = new ChatClient(endpoint, communicationTokenCredential);
211-
}
212-
}
213-
}
214-
```
215-
216-
### Start a chat thread with the bot
217-
218-
Use the `createChatThread` method on `chatClient` to create a chat thread. Replace the ID with the bot's Communication Services ID.
219-
220-
```csharp
221-
var chatParticipant = new ChatParticipant(identifier: new CommunicationUserIdentifier(id: "<BOT_ID>"))
222-
{
223-
DisplayName = "BotDisplayName"
224-
};
225-
CreateChatThreadResult createChatThreadResult = await chatClient.CreateChatThreadAsync(topic: "Hello Bot!", participants: new[] { chatParticipant });
226-
ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId: createChatThreadResult.ChatThread.Id);
227-
string threadId = chatThreadClient.Id;
228-
```
229-
230-
### Get a chat thread client
231-
232-
The `GetChatThreadClient` method returns a thread client for a thread that already exists:
233-
234-
```csharp
235-
string threadId = "<THREAD_ID>";
236-
ChatThreadClient chatThreadClient = chatClient.GetChatThreadClient(threadId: threadId);
237-
```
238-
239-
### Send a message to a chat thread
240-
241-
To use `SendMessage` to send a message to a thread:
242-
243-
```csharp
244-
SendChatMessageOptions sendChatMessageOptions = new SendChatMessageOptions()
245-
{
246-
Content = "Hello World",
247-
MessageType = ChatMessageType.Text
248-
};
249-
250-
SendChatMessageResult sendChatMessageResult = await chatThreadClient.SendMessageAsync(sendChatMessageOptions);
251-
252-
string messageId = sendChatMessageResult.Id;
253-
```
254-
255-
### Receive chat messages from a chat thread
256-
257-
You can get chat messages by polling the `GetMessages` method on the chat thread client at set intervals:
258-
259-
```csharp
260-
AsyncPageable<ChatMessage> allMessages = chatThreadClient.GetMessagesAsync();
261-
await foreach (ChatMessage message in allMessages)
262-
{
263-
Console.WriteLine($"{message.Id}:{message.Content.Message}");
264-
}
187+
dotnet run
265188
```
266189

267-
Check the list of messages for the bot's echo reply to "Hello World".
190+
You should receive a message from the bot in the console that says "Hello World".
268191

269-
You can use JavaScript or the Azure mobile SDKs to subscribe to incoming message notifications:
270-
271-
```javascript
272-
// Open notifications channel
273-
await chatClient.startRealtimeNotifications();
274-
// Subscribe to new notifications
275-
chatClient.on("chatMessageReceived", (e) => {
276-
console.log("Notification chatMessageReceived!");
277-
// Your code here
278-
});
279-
```
192+
Example output:
280193

281-
### Clean up the chat thread
282-
283-
When you're finished using the chat thread, delete the thread:
284-
285-
```csharp
286-
chatClient.DeleteChatThread(threadId);
194+
```powershell
195+
1730405535010:Hello World
287196
```
288197

289-
### Deploy the C# chat application
290-
291-
To deploy the chat application:
292-
293-
1. In Visual Studio, open the chat project.
294-
295-
1. Right-click the **ChatQuickstart** project and select **Publish**:
296-
297-
:::image type="content" source="./media/deploy-chat-application.png" alt-text="Screenshot that shows deploying the chat application to Azure from Visual Studio.":::
298-
299-
1. Once you publish the solution, run it and check if Echobot echoes the user message on the command prompt. Now that you have the solution you can proceed to play with the various activities that are needed for the business scenarios that you need to solve for.
300-
301198
## More things you can do with a bot
302199

303200
A bot can receive more than a plain-text message from a user in a Communications Services Chat channel. Some of the activities a bot can receive from a user include:
@@ -560,7 +457,3 @@ Verify that the bot's Microsoft app ID and password are saved correctly in the b
560457
### Bot can't be added as a participant
561458

562459
Verify that the bot's Communication Services ID is used correctly when a request is sent to add a bot to a chat thread.
563-
564-
## Next steps
565-
566-
Try the [chat bot demo app](https://github.com/Azure/communication-preview/tree/master/samples/AzureBotService-Sample-App) for a 1:1 chat between a chat user and a bot via the BotFramework WebChat UI component.

0 commit comments

Comments
 (0)