Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public class BotFrameworkAdapter extends BotAdapter
*/
private Map<String, ConnectorClient> connectorClients = new ConcurrentHashMap<>();

/**
* OAuthClient cache.
*/
private Map<String, OAuthClient> oAuthClients = new ConcurrentHashMap<>();

/**
* Initializes a new instance of the {@link BotFrameworkAdapter} class, using a
* credential provider.
Expand Down Expand Up @@ -1287,31 +1292,37 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
}

String appId = getBotAppId(turnContext);
String cacheKey = appId + (oAuthAppCredentials != null ? oAuthAppCredentials.getAppId() : "");
String oAuthScope = getBotFrameworkOAuthScope();
AppCredentials credentials = oAuthAppCredentials != null
? oAuthAppCredentials
: getAppCredentials(appId, oAuthScope).join();

OAuthClient oAuthClient = new RestOAuthClient(
OAuthClientConfig.emulateOAuthCards
? turnContext.getActivity().getServiceUrl()
: OAuthClientConfig.OAUTHENDPOINT,
credentials
);
OAuthClient client = oAuthClients.computeIfAbsent(cacheKey, key -> {
OAuthClient oAuthClient = new RestOAuthClient(
OAuthClientConfig.emulateOAuthCards
? turnContext.getActivity().getServiceUrl()
: OAuthClientConfig.OAUTHENDPOINT,
credentials
);

if (OAuthClientConfig.emulateOAuthCards) {
// do not join task - we want this to run in the background.
OAuthClientConfig
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards);
}

return oAuthClient;
});

// adding the oAuthClient into the TurnState
// TokenResolver.cs will use it get the correct credentials to poll for
// token for streaming scenario
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient);

if (OAuthClientConfig.emulateOAuthCards) {
// do not join task - we want this to run in the background.
return OAuthClientConfig
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards)
.thenApply(result -> oAuthClient);
if (turnContext.getTurnState().get(BotAdapter.OAUTH_CLIENT_KEY) == null) {
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, client);
}

return CompletableFuture.completedFuture(oAuthClient);
return CompletableFuture.completedFuture(client);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

Expand Down Expand Up @@ -105,4 +106,15 @@ public static <T> T convert(Object source, Class<T> toClass) {
public static String toString(Object source) throws JsonProcessingException {
return objectMapper.writeValueAsString(source);
}

/**
* Parses a JSON document.
*
* @param json The JSON to parse.
* @return A JsonNode containg the node tree.
* @throws IOException Error parsing json.
*/
public static JsonNode jsonToTree(String json) throws IOException {
return objectMapper.readTree(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -112,6 +113,14 @@ public void setImages(List<CardImage> withImages) {
this.images = withImages;
}

/**
* Sets the images list with a single image.
* @param image The image to set as the list.
*/
public void setImage(CardImage image) {
setImages(Collections.singletonList(image));
}

/**
* Get the buttons value.
*
Expand Down Expand Up @@ -150,7 +159,7 @@ public void setTap(CardAction withTap) {

/**
* Creates an @{link Attachment} for this card.
*
*
* @return An Attachment object containing the card.
*/
public Attachment toAttachment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MessagingExtensionResult {
/**
* Gets hint for how to deal with multiple attachments. Possible values include:
* 'list', 'grid'
*
*
* @return The attachment layout hint.
*/
public String getAttachmentLayout() {
Expand All @@ -49,7 +49,7 @@ public String getAttachmentLayout() {
/**
* Sets hint for how to deal with multiple attachments. Possible values include:
* 'list', 'grid'
*
*
* @param withAttachmentLayout The attachment layout hint.
*/
public void setAttachmentLayout(String withAttachmentLayout) {
Expand All @@ -59,7 +59,7 @@ public void setAttachmentLayout(String withAttachmentLayout) {
/**
* Gets the type of the result. Possible values include: 'result', 'auth',
* 'config', 'message', 'botMessagePreview'
*
*
* @return The result type.
*/
public String getType() {
Expand All @@ -69,7 +69,7 @@ public String getType() {
/**
* Sets the type of the result. Possible values include: 'result', 'auth',
* 'config', 'message', 'botMessagePreview'
*
*
* @param withType The result type.
*/
public void setType(String withType) {
Expand All @@ -78,7 +78,7 @@ public void setType(String withType) {

/**
* Gets (Only when type is result) Attachments.
*
*
* @return The result attachments.
*/
public List<MessagingExtensionAttachment> getAttachments() {
Expand All @@ -88,7 +88,7 @@ public List<MessagingExtensionAttachment> getAttachments() {
/**
* Sets (Only when type is result) Attachments. This replaces all previous
* attachments on the object.
*
*
* @param withAttachments The result attachments.
*/
public void setAttachments(List<MessagingExtensionAttachment> withAttachments) {
Expand All @@ -98,7 +98,7 @@ public void setAttachments(List<MessagingExtensionAttachment> withAttachments) {
/**
* Sets (Only when type is result) Attachments to the specific attachment. This
* replaces all previous attachments on the object.
*
*
* @param withAttachment The attachment.
*/
public void setAttachment(MessagingExtensionAttachment withAttachment) {
Expand All @@ -107,7 +107,7 @@ public void setAttachment(MessagingExtensionAttachment withAttachment) {

/**
* Gets (Only when type is auth or config) suggested actions.
*
*
* @return The suggested actions.
*/
public MessagingExtensionSuggestedAction getSuggestedActions() {
Expand All @@ -116,7 +116,7 @@ public MessagingExtensionSuggestedAction getSuggestedActions() {

/**
* Sets (Only when type is auth or config) suggested actions.
*
*
* @param withSuggestedActions The suggested actions.
*/
public void setSuggestedActions(MessagingExtensionSuggestedAction withSuggestedActions) {
Expand All @@ -125,7 +125,7 @@ public void setSuggestedActions(MessagingExtensionSuggestedAction withSuggestedA

/**
* Gets (Only when type is message) Text.
*
*
* @return The result text.
*/
public String getText() {
Expand All @@ -134,7 +134,7 @@ public String getText() {

/**
* Sets (Only when type is message) Text.
*
*
* @param withText The result text.
*/
public void setText(String withText) {
Expand All @@ -143,7 +143,7 @@ public void setText(String withText) {

/**
* Gets (Only when type is botMessagePreview) Message activity.
*
*
* @return The preview Activity.
*/
public Activity getActivityPreview() {
Expand All @@ -152,7 +152,7 @@ public Activity getActivityPreview() {

/**
* Sets (Only when type is botMessagePreview) Message activity.
*
*
* @param withActivityPreview The preview Activity.
*/
public void setActivityPreview(Activity withActivityPreview) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.microsoft.bot.schema.CardAction;

import java.util.Collections;
import java.util.List;

/**
Expand All @@ -19,7 +20,7 @@ public class MessagingExtensionSuggestedAction {

/**
* Gets the actions.
*
*
* @return The list of CardActions.
*/
public List<CardAction> getActions() {
Expand All @@ -28,10 +29,18 @@ public List<CardAction> getActions() {

/**
* Sets the actions.
*
*
* @param withActions The list of CardActions.
*/
public void setActions(List<CardAction> withActions) {
actions = withActions;
}

/**
* Sets the list of actions to a single specified CardAction.
* @param action The CardAction
*/
public void setAction(CardAction action) {
setActions(Collections.singletonList(action));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ public class TaskModuleContinueResponse extends TaskModuleResponseBase {
@JsonProperty(value = "value")
private TaskModuleTaskInfo value;

/**
* Initializes a new instance.
*/
public TaskModuleContinueResponse() {
setType("continue");
}

/**
* Gets the Adaptive card to appear in the task module.
*
*
* @return The value info.
*/
public TaskModuleTaskInfo getValue() {
Expand All @@ -23,7 +30,7 @@ public TaskModuleTaskInfo getValue() {

/**
* Sets the Adaptive card to appear in the task module.
*
*
* @param withValue The value info.
*/
public void setValue(TaskModuleTaskInfo withValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ public class TaskModuleMessageResponse extends TaskModuleResponseBase {
@JsonProperty(value = "value")
private String value;

/**
* Initializes a new instance.
*/
public TaskModuleMessageResponse() {
setType("message");
}

/**
* Gets info teams will display the value of value in a popup message box.
*
*
* @return The popup info.
*/
public String getValue() {
Expand All @@ -23,7 +30,7 @@ public String getValue() {

/**
* Sets info teams will display the value of value in a popup message box.
*
*
* @param withValue The popup info.
*/
public void setValue(String withValue) {
Expand Down
20 changes: 5 additions & 15 deletions samples/52.teams-messaging-extensions-search-auth-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ the Teams service needs to call into the bot.
- Use the current `https` URL you were given by running ngrok. Append with the path `/api/messages` used by this sample
- 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)
- __*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)

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)

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.)
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.)

1) __*This step is specific to Teams.*__
- **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`)
Expand All @@ -46,21 +48,9 @@ the Teams service needs to call into the bot.
- Run it by using `java -jar .\target\bot-teams-messaging-extensions-search-auth-config-sample.jar`


## Interacting with the bot
## Interacting with the bot in Teams

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.

1. **Show Welcome**
- **Result:** The bot will send the welcome card for you to interact with
- **Valid Scopes:** personal, group chat, team chat
2. **MentionMe**
- **Result:** The bot will respond to the message and mention the user
- **Valid Scopes:** personal, group chat, team chat
3. **MessageAllMembers**
- **Result:** The bot will send a 1-on-1 message to each member in the current conversation (aka on the conversation's roster).
- **Valid Scopes:** personal, group chat, team chat

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.
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.

### Avoiding Permission-Related Errors

Expand Down
13 changes: 12 additions & 1 deletion samples/52.teams-messaging-extensions-search-auth-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -82,7 +87,13 @@
<version>4.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
<scope>compile</scope>
</dependency>
</dependencies>

<repositories>
<repository>
Expand Down
Loading