Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit ba955fb

Browse files
authored
Merge pull request #475 from microsoft/emimunoz/52-search-auth
52-Search-Auth
2 parents f27e829 + b936f89 commit ba955fb

14 files changed

Lines changed: 560 additions & 214 deletions

File tree

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public class BotFrameworkAdapter extends BotAdapter
124124
*/
125125
private Map<String, ConnectorClient> connectorClients = new ConcurrentHashMap<>();
126126

127+
/**
128+
* OAuthClient cache.
129+
*/
130+
private Map<String, OAuthClient> oAuthClients = new ConcurrentHashMap<>();
131+
127132
/**
128133
* Initializes a new instance of the {@link BotFrameworkAdapter} class, using a
129134
* credential provider.
@@ -1287,31 +1292,37 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
12871292
}
12881293

12891294
String appId = getBotAppId(turnContext);
1295+
String cacheKey = appId + (oAuthAppCredentials != null ? oAuthAppCredentials.getAppId() : "");
12901296
String oAuthScope = getBotFrameworkOAuthScope();
12911297
AppCredentials credentials = oAuthAppCredentials != null
12921298
? oAuthAppCredentials
12931299
: getAppCredentials(appId, oAuthScope).join();
12941300

1295-
OAuthClient oAuthClient = new RestOAuthClient(
1296-
OAuthClientConfig.emulateOAuthCards
1297-
? turnContext.getActivity().getServiceUrl()
1298-
: OAuthClientConfig.OAUTHENDPOINT,
1299-
credentials
1300-
);
1301+
OAuthClient client = oAuthClients.computeIfAbsent(cacheKey, key -> {
1302+
OAuthClient oAuthClient = new RestOAuthClient(
1303+
OAuthClientConfig.emulateOAuthCards
1304+
? turnContext.getActivity().getServiceUrl()
1305+
: OAuthClientConfig.OAUTHENDPOINT,
1306+
credentials
1307+
);
1308+
1309+
if (OAuthClientConfig.emulateOAuthCards) {
1310+
// do not join task - we want this to run in the background.
1311+
OAuthClientConfig
1312+
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards);
1313+
}
1314+
1315+
return oAuthClient;
1316+
});
13011317

13021318
// adding the oAuthClient into the TurnState
13031319
// TokenResolver.cs will use it get the correct credentials to poll for
13041320
// token for streaming scenario
1305-
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient);
1306-
1307-
if (OAuthClientConfig.emulateOAuthCards) {
1308-
// do not join task - we want this to run in the background.
1309-
return OAuthClientConfig
1310-
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards)
1311-
.thenApply(result -> oAuthClient);
1321+
if (turnContext.getTurnState().get(BotAdapter.OAUTH_CLIENT_KEY) == null) {
1322+
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, client);
13121323
}
13131324

1314-
return CompletableFuture.completedFuture(oAuthClient);
1325+
return CompletableFuture.completedFuture(client);
13151326
}
13161327

13171328
/**

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Serialization.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.JsonNode;
99
import com.fasterxml.jackson.databind.ObjectMapper;
1010

11+
import java.io.IOException;
1112
import java.util.concurrent.CompletableFuture;
1213
import java.util.concurrent.CompletionException;
1314

@@ -105,4 +106,15 @@ public static <T> T convert(Object source, Class<T> toClass) {
105106
public static String toString(Object source) throws JsonProcessingException {
106107
return objectMapper.writeValueAsString(source);
107108
}
109+
110+
/**
111+
* Parses a JSON document.
112+
*
113+
* @param json The JSON to parse.
114+
* @return A JsonNode containg the node tree.
115+
* @throws IOException Error parsing json.
116+
*/
117+
public static JsonNode jsonToTree(String json) throws IOException {
118+
return objectMapper.readTree(json);
119+
}
108120
}

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/ThumbnailCard.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.annotation.JsonInclude;
88
import com.fasterxml.jackson.annotation.JsonProperty;
99

10+
import java.util.Collections;
1011
import java.util.List;
1112

1213
/**
@@ -112,6 +113,14 @@ public void setImages(List<CardImage> withImages) {
112113
this.images = withImages;
113114
}
114115

116+
/**
117+
* Sets the images list with a single image.
118+
* @param image The image to set as the list.
119+
*/
120+
public void setImage(CardImage image) {
121+
setImages(Collections.singletonList(image));
122+
}
123+
115124
/**
116125
* Get the buttons value.
117126
*
@@ -150,7 +159,7 @@ public void setTap(CardAction withTap) {
150159

151160
/**
152161
* Creates an @{link Attachment} for this card.
153-
*
162+
*
154163
* @return An Attachment object containing the card.
155164
*/
156165
public Attachment toAttachment() {

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/teams/MessagingExtensionResult.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MessagingExtensionResult {
3939
/**
4040
* Gets hint for how to deal with multiple attachments. Possible values include:
4141
* 'list', 'grid'
42-
*
42+
*
4343
* @return The attachment layout hint.
4444
*/
4545
public String getAttachmentLayout() {
@@ -49,7 +49,7 @@ public String getAttachmentLayout() {
4949
/**
5050
* Sets hint for how to deal with multiple attachments. Possible values include:
5151
* 'list', 'grid'
52-
*
52+
*
5353
* @param withAttachmentLayout The attachment layout hint.
5454
*/
5555
public void setAttachmentLayout(String withAttachmentLayout) {
@@ -59,7 +59,7 @@ public void setAttachmentLayout(String withAttachmentLayout) {
5959
/**
6060
* Gets the type of the result. Possible values include: 'result', 'auth',
6161
* 'config', 'message', 'botMessagePreview'
62-
*
62+
*
6363
* @return The result type.
6464
*/
6565
public String getType() {
@@ -69,7 +69,7 @@ public String getType() {
6969
/**
7070
* Sets the type of the result. Possible values include: 'result', 'auth',
7171
* 'config', 'message', 'botMessagePreview'
72-
*
72+
*
7373
* @param withType The result type.
7474
*/
7575
public void setType(String withType) {
@@ -78,7 +78,7 @@ public void setType(String withType) {
7878

7979
/**
8080
* Gets (Only when type is result) Attachments.
81-
*
81+
*
8282
* @return The result attachments.
8383
*/
8484
public List<MessagingExtensionAttachment> getAttachments() {
@@ -88,7 +88,7 @@ public List<MessagingExtensionAttachment> getAttachments() {
8888
/**
8989
* Sets (Only when type is result) Attachments. This replaces all previous
9090
* attachments on the object.
91-
*
91+
*
9292
* @param withAttachments The result attachments.
9393
*/
9494
public void setAttachments(List<MessagingExtensionAttachment> withAttachments) {
@@ -98,7 +98,7 @@ public void setAttachments(List<MessagingExtensionAttachment> withAttachments) {
9898
/**
9999
* Sets (Only when type is result) Attachments to the specific attachment. This
100100
* replaces all previous attachments on the object.
101-
*
101+
*
102102
* @param withAttachment The attachment.
103103
*/
104104
public void setAttachment(MessagingExtensionAttachment withAttachment) {
@@ -107,7 +107,7 @@ public void setAttachment(MessagingExtensionAttachment withAttachment) {
107107

108108
/**
109109
* Gets (Only when type is auth or config) suggested actions.
110-
*
110+
*
111111
* @return The suggested actions.
112112
*/
113113
public MessagingExtensionSuggestedAction getSuggestedActions() {
@@ -116,7 +116,7 @@ public MessagingExtensionSuggestedAction getSuggestedActions() {
116116

117117
/**
118118
* Sets (Only when type is auth or config) suggested actions.
119-
*
119+
*
120120
* @param withSuggestedActions The suggested actions.
121121
*/
122122
public void setSuggestedActions(MessagingExtensionSuggestedAction withSuggestedActions) {
@@ -125,7 +125,7 @@ public void setSuggestedActions(MessagingExtensionSuggestedAction withSuggestedA
125125

126126
/**
127127
* Gets (Only when type is message) Text.
128-
*
128+
*
129129
* @return The result text.
130130
*/
131131
public String getText() {
@@ -134,7 +134,7 @@ public String getText() {
134134

135135
/**
136136
* Sets (Only when type is message) Text.
137-
*
137+
*
138138
* @param withText The result text.
139139
*/
140140
public void setText(String withText) {
@@ -143,7 +143,7 @@ public void setText(String withText) {
143143

144144
/**
145145
* Gets (Only when type is botMessagePreview) Message activity.
146-
*
146+
*
147147
* @return The preview Activity.
148148
*/
149149
public Activity getActivityPreview() {
@@ -152,7 +152,7 @@ public Activity getActivityPreview() {
152152

153153
/**
154154
* Sets (Only when type is botMessagePreview) Message activity.
155-
*
155+
*
156156
* @param withActivityPreview The preview Activity.
157157
*/
158158
public void setActivityPreview(Activity withActivityPreview) {

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/teams/MessagingExtensionSuggestedAction.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.annotation.JsonProperty;
88
import com.microsoft.bot.schema.CardAction;
99

10+
import java.util.Collections;
1011
import java.util.List;
1112

1213
/**
@@ -19,7 +20,7 @@ public class MessagingExtensionSuggestedAction {
1920

2021
/**
2122
* Gets the actions.
22-
*
23+
*
2324
* @return The list of CardActions.
2425
*/
2526
public List<CardAction> getActions() {
@@ -28,10 +29,18 @@ public List<CardAction> getActions() {
2829

2930
/**
3031
* Sets the actions.
31-
*
32+
*
3233
* @param withActions The list of CardActions.
3334
*/
3435
public void setActions(List<CardAction> withActions) {
3536
actions = withActions;
3637
}
38+
39+
/**
40+
* Sets the list of actions to a single specified CardAction.
41+
* @param action The CardAction
42+
*/
43+
public void setAction(CardAction action) {
44+
setActions(Collections.singletonList(action));
45+
}
3746
}

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/teams/TaskModuleContinueResponse.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ public class TaskModuleContinueResponse extends TaskModuleResponseBase {
1212
@JsonProperty(value = "value")
1313
private TaskModuleTaskInfo value;
1414

15+
/**
16+
* Initializes a new instance.
17+
*/
18+
public TaskModuleContinueResponse() {
19+
setType("continue");
20+
}
21+
1522
/**
1623
* Gets the Adaptive card to appear in the task module.
17-
*
24+
*
1825
* @return The value info.
1926
*/
2027
public TaskModuleTaskInfo getValue() {
@@ -23,7 +30,7 @@ public TaskModuleTaskInfo getValue() {
2330

2431
/**
2532
* Sets the Adaptive card to appear in the task module.
26-
*
33+
*
2734
* @param withValue The value info.
2835
*/
2936
public void setValue(TaskModuleTaskInfo withValue) {

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/teams/TaskModuleMessageResponse.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ public class TaskModuleMessageResponse extends TaskModuleResponseBase {
1212
@JsonProperty(value = "value")
1313
private String value;
1414

15+
/**
16+
* Initializes a new instance.
17+
*/
18+
public TaskModuleMessageResponse() {
19+
setType("message");
20+
}
21+
1522
/**
1623
* Gets info teams will display the value of value in a popup message box.
17-
*
24+
*
1825
* @return The popup info.
1926
*/
2027
public String getValue() {
@@ -23,7 +30,7 @@ public String getValue() {
2330

2431
/**
2532
* Sets info teams will display the value of value in a popup message box.
26-
*
33+
*
2734
* @param withValue The popup info.
2835
*/
2936
public void setValue(String withValue) {

samples/52.teams-messaging-extensions-search-auth-config/README.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ the Teams service needs to call into the bot.
3232
- Use the current `https` URL you were given by running ngrok. Append with the path `/api/messages` used by this sample
3333
- Ensure that you've [enabled the Teams Channel](https://docs.microsoft.com/en-us/azure/bot-service/channel-connect-teams?view=azure-bot-service-4.0)
3434
- __*If you don't have an Azure account*__ you can use this [Bot Framework registration](https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/create-a-bot-for-teams#register-your-web-service-with-the-bot-framework)
35+
36+
1) [Add Authentication to your bot via Azure Bot Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=csharp)
3537

36-
1) Update the `resources/application.properties` configuration for the bot to use the Microsoft App Id and App Password from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.)
38+
1) Update the `resources/application.properties` configuration for the bot to use the `MicrosoftAppId`, `MicrosfotAppPassword`, `ConnectionName`, and `SiteUrl` from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.)
3739

3840
1) __*This step is specific to Teams.*__
3941
- **Edit** the `manifest.json` contained in the `teamsAppManifest` folder to replace your Microsoft App Id (that was created when you registered your bot earlier) *everywhere* you see the place holder string `<<YOUR-MICROSOFT-APP-ID>>` (depending on the scenario the Microsoft App Id may occur multiple times in the `manifest.json`)
@@ -46,21 +48,9 @@ the Teams service needs to call into the bot.
4648
- Run it by using `java -jar .\target\bot-teams-messaging-extensions-search-auth-config-sample.jar`
4749
4850
49-
## Interacting with the bot
51+
## Interacting with the bot in Teams
5052
51-
You can interact with this bot by sending it a message, or selecting a command from the command list. The bot will respond to the following strings.
52-
53-
1. **Show Welcome**
54-
- **Result:** The bot will send the welcome card for you to interact with
55-
- **Valid Scopes:** personal, group chat, team chat
56-
2. **MentionMe**
57-
- **Result:** The bot will respond to the message and mention the user
58-
- **Valid Scopes:** personal, group chat, team chat
59-
3. **MessageAllMembers**
60-
- **Result:** The bot will send a 1-on-1 message to each member in the current conversation (aka on the conversation's roster).
61-
- **Valid Scopes:** personal, group chat, team chat
62-
63-
You can select an option from the command list by typing ```@TeamsConversationBot``` into the compose message area and ```What can I do?``` text above the compose area.
53+
Once the Messaging Extension is installed, click the icon for **Config Auth Search** in the Compose Box's Messaging Extension menu to display the search window. Left click to choose **Settings** and view the Config page.
6454
6555
### Avoiding Permission-Related Errors
6656

samples/52.teams-messaging-extensions-search-auth-config/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
</properties>
4949

5050
<dependencies>
51+
<dependency>
52+
<groupId>com.microsoft.graph</groupId>
53+
<artifactId>microsoft-graph</artifactId>
54+
<version>1.7.1</version>
55+
</dependency>
5156
<dependency>
5257
<groupId>junit</groupId>
5358
<artifactId>junit</artifactId>
@@ -82,7 +87,13 @@
8287
<version>4.0.0-SNAPSHOT</version>
8388
<scope>compile</scope>
8489
</dependency>
85-
</dependencies>
90+
<dependency>
91+
<groupId>org.json</groupId>
92+
<artifactId>json</artifactId>
93+
<version>20190722</version>
94+
<scope>compile</scope>
95+
</dependency>
96+
</dependencies>
8697

8798
<repositories>
8899
<repository>

0 commit comments

Comments
 (0)