Skip to content

Commit 5a8ea06

Browse files
authored
Josh/link (microsoft#1882)
* adding link unfurling sample * updating readme
1 parent 4c794e4 commit 5a8ea06

12 files changed

+730
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Microsoft.BotBuilderSamples
9+
{
10+
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
11+
{
12+
public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger)
13+
: base(configuration, logger)
14+
{
15+
OnTurnError = async (turnContext, exception) =>
16+
{
17+
// Log any leaked exception from the application.
18+
logger.LogError($"Exception caught : {exception.Message}");
19+
20+
// Send a catch-all apology to the user.
21+
await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");
22+
};
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.Bot.Builder;
8+
using Microsoft.Bot.Builder.Teams;
9+
using Microsoft.Bot.Schema;
10+
using Microsoft.Bot.Schema.Teams;
11+
12+
namespace Microsoft.BotBuilderSamples.Bots
13+
{
14+
/*
15+
* This one exists in JS.
16+
*/
17+
public class LinkUnfurlingBot : TeamsActivityHandler
18+
{
19+
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
20+
{
21+
await turnContext.SendActivityAsync(MessageFactory.Text($"echo: {turnContext.Activity.Text}"), cancellationToken);
22+
await turnContext.SendActivityAsync(
23+
MessageFactory.Text("If you copy and paste a link from seattletimes.com the link will unfurl."), cancellationToken);
24+
}
25+
26+
protected override async Task<MessagingExtensionResponse> OnTeamsAppBasedLinkQueryAsync(ITurnContext<IInvokeActivity> turnContext, AppBasedLinkQuery query, CancellationToken cancellationToken)
27+
{
28+
var heroCard = new ThumbnailCard
29+
{
30+
Title = "Thumbnail Card",
31+
Text = query.Url,
32+
Images = new List<CardImage> { new CardImage("https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png") },
33+
};
34+
35+
var attachments = new MessagingExtensionAttachment(HeroCard.ContentType, null, heroCard);
36+
var result = new MessagingExtensionResult(AttachmentLayoutTypes.List, "result", new[] { attachments }, null, "test unfurl");
37+
38+
return new MessagingExtensionResponse(result);
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Bot.Builder;
7+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
8+
9+
namespace Microsoft.BotBuilderSamples.Controllers
10+
{
11+
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
12+
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
13+
// achieved by specifying a more specific type for the bot constructor argument.
14+
[Route("api/messages")]
15+
[ApiController]
16+
public class BotController : ControllerBase
17+
{
18+
private readonly IBotFrameworkHttpAdapter Adapter;
19+
private readonly IBot Bot;
20+
21+
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
22+
{
23+
Adapter = adapter;
24+
Bot = bot;
25+
}
26+
27+
[HttpPost]
28+
public async Task PostAsync()
29+
{
30+
// Delegate the processing of the HTTP POST to the adapter.
31+
// The adapter will invoke the bot.
32+
await Adapter.ProcessAsync(Request, Response, Bot);
33+
}
34+
}
35+
}

samples/csharp_dotnetcore/55.teams-link-unfurling/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public class Program
1010
{
1111
public static void Main(string[] args)
1212
{
13+
CreateWebHostBuilder(args).Build().Run();
1314
}
15+
16+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
17+
WebHost.CreateDefaultBuilder(args)
18+
.UseStartup<Startup>();
1419
}
1520
}

samples/csharp_dotnetcore/55.teams-link-unfurling/Properties/launchSettings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:10754/",
6+
"applicationUrl": "http://localhost:3978/",
77
"sslPort": 0
88
}
99
},
@@ -15,13 +15,13 @@
1515
"ASPNETCORE_ENVIRONMENT": "Development"
1616
}
1717
},
18-
"TeamsLinkUnfurling": {
18+
"LinkUnfurling": {
1919
"commandName": "Project",
2020
"launchBrowser": true,
2121
"environmentVariables": {
2222
"ASPNETCORE_ENVIRONMENT": "Development"
2323
},
24-
"applicationUrl": "http://localhost:10755/"
24+
"applicationUrl": "http://localhost:3978/"
2525
}
2626
}
2727
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,72 @@
1-
# EchoBot
1+
## TeamsLinkUnfurlingBot
22

3-
Bot Framework v4 Teams Link Unfurling.
3+
Bot Framework v4 Teams link unfurling bot sample for Teams.
44

55
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that performs link unfurling in Teams.
66

77
## Prerequisites
88

9+
910
- [.NET Core SDK](https://dotnet.microsoft.com/download) version 2.1
1011

1112
```bash
1213
# determine dotnet version
1314
dotnet --version
1415
```
16+
17+
- Microsoft Teams is installed and you have an account
1518

1619
## To try this sample
1720

21+
### Clone the repo
1822
- Clone the repository
1923

2024
```bash
2125
git clone https://github.com/Microsoft/botbuilder-samples.git
2226
```
2327

24-
- In a terminal, navigate to `samples/csharp_dotnetcore/55.teams-link-unfurling`
25-
- Run the bot from a terminal or from Visual Studio, choose option A or B.
26-
27-
A) From a terminal
28+
### Ngrok
29+
- Download and install [ngrok](https://ngrok.com/download)
30+
- In terminal navigate to where ngrok is installed and run:
31+
32+
```bash
33+
ngrok http -host-header=rewrite 3978
34+
```
35+
- Copy/paste the ```https``` **NOT** the ```http``` web address into notepad as you will need it later
36+
37+
### Creating the bot registration
38+
- Create a new bot [here](https://dev.botframework.com/bots/new)
39+
- Enter a```Display name``` and ```Bot handle```
40+
- In the ```Messaging endpoint``` enter the https address from Ngrok and add ```/api/messages``` to the end
41+
- EX: ```https://7d899fbb.ngrok.io/api/messages```
42+
- Open the ```Create Microsoft App ID and password``` link in a new tab
43+
- Click on the ```New registration``` button
44+
- Enter a name, and select the ```Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)```
45+
- Click ```Register```
46+
- Copy & paste the ```Application (client) ID``` field into notepad. This is your botID.
47+
- Click on ```Certificates & secrets``` tab on the left
48+
- Click ```New client secret```
49+
- Enter a name, select `Never`, and click ```Add```
50+
- Copy & paste the password into notepad. This is your app password.
51+
- Go back to the bot registration tab and enter the ```botID``` into the app ID field
52+
- Scroll down, agree to the Terms, and click ```Register```
53+
- Click the ```Microsoft Teams``` icon on the next screen
54+
- Click ```Save```
55+
56+
### Visual Studio
57+
- Launch Visual Studio
58+
- Navigate to and open the `samples/csharp_dotnet/55.teams-link-unfurling-bot` directory
59+
- Open the ```appsettings.json``` file
60+
- Paste your botID value into the ```MicrosoftAppId``` field
61+
- Put the password into the ```MicrosoftAppPassword``` field
62+
- Save the file
63+
- Open the ```manifest.json```
64+
- Replace your botID everywhere you see the place holder string ```<<YOUR-MICROSOFT-BOT-ID>>```
65+
66+
67+
- Run the bot:
68+
69+
A) From a terminal
2870
2971
```bash
3072
# run the bot
@@ -33,36 +75,25 @@ This bot has been created using [Bot Framework](https://dev.botframework.com), i
3375
3476
B) Or from Visual Studio
3577
36-
- Launch Visual Studio
3778
- File -> Open -> Project/Solution
38-
- Navigate to `samples/csharp_dotnetcore/55.teams-link-unfurling` folder
39-
- Select `TeamsLinkUnfurling.csproj` file
79+
- Navigate to `samples/csharp_dotnetcore/55.teams-link-unfurling-bot` folder
80+
- Select `TeamsLinkUnfurlingBot.csproj` file
4081
- Press `F5` to run the project
4182
42-
## Testing the bot using Teams
43-
44-
1) run ngrok - point to port 3978
45-
1) create bot framework registration - using ngrok URL
46-
1) update your manifest.json to include the app id from bot framework
47-
1) zip up teams-manifest folder to create a manifest.zip
48-
1) upload manifest.zip to teams (from Apps view click "Upload a custom app")
49-
1) pick your bot from the compose command menu
50-
51-
## Deploy the bot to Azure
52-
53-
To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions.
54-
55-
## Further reading
56-
57-
- [Bot Framework Documentation](https://docs.botframework.com)
58-
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
59-
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
60-
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
61-
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
62-
- [.NET Core CLI tools](https://docs.microsoft.com/en-us/dotnet/core/tools/?tabs=netcore2x)
63-
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
64-
- [Azure Portal](https://portal.azure.com)
65-
- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/)
66-
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
67-
- [Restify](https://www.npmjs.com/package/restify)
68-
- [dotenv](https://www.npmjs.com/package/dotenv)
83+
### Teams - App Studio
84+
- Launch Microsoft Teams
85+
- In the bar at the top of Teams search for and select ```App Studio```
86+
- Click the ```Manifest editor``` tab
87+
- Click ```Import an existing app```
88+
- Navigate to and select the `manifest.json` file from the previous step
89+
- Click on the `TeamsConversationBot` card
90+
- Click ```Test and distribute``` on the left hand side
91+
- Click the ```Install``` button
92+
93+
| To install bot in a personal chat... | To install in a group chat... | To install in team chat... |
94+
|:-------------------- | :------------------------- | :-----------------------|
95+
| 1. Click ```Add``` button| 1. Click the down arrow to the right of the ```Add``` button <br> 2. Click ```Add to Chat``` <br> 3. Search for and select your group chat <br> 4. Click the ```Set up bot``` button <br> **Note:** There must be at least 1 message in a group chat for it to be searchable | 1. Click the down arrow to the right of the ```Add``` button <br> 2. Click ```Add to Team``` <br> 3. Search for and select your team <br> 4. Click the ```Set up a bot``` button |
96+
97+
### Interacting with the bot
98+
99+
If you copy and paste a link from https://www.seattletimes.com into the compose message area the link will unfurl.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Bot.Builder;
8+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
9+
using Microsoft.BotBuilderSamples.Bots;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
12+
13+
namespace Microsoft.BotBuilderSamples
14+
{
15+
public class Startup
16+
{
17+
public Startup(IConfiguration configuration)
18+
{
19+
Configuration = configuration;
20+
}
21+
22+
public IConfiguration Configuration { get; }
23+
24+
// This method gets called by the runtime. Use this method to add services to the container.
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
28+
29+
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
30+
services.AddSingleton<IStorage, MemoryStorage>();
31+
32+
// Create the Bot Framework Adapter with error handling enabled.
33+
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
34+
35+
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
36+
services.AddTransient<IBot, LinkUnfurlingBot>();
37+
}
38+
39+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
40+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
41+
{
42+
if (env.IsDevelopment())
43+
{
44+
app.UseDeveloperExceptionPage();
45+
}
46+
else
47+
{
48+
app.UseHsts();
49+
}
50+
51+
app.UseDefaultFiles();
52+
app.UseStaticFiles();
53+
54+
//app.UseHttpsRedirection();
55+
app.UseMvc();
56+
}
57+
}
58+
}

samples/csharp_dotnetcore/55.teams-link-unfurling/TeamsLinkUnfurling.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MicrosoftAppId": "",
3+
"MicrosoftAppPassword": ""
4+
}

0 commit comments

Comments
 (0)