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

Update teams members added to use TeamsInfo.getMember() #514

Merged
merged 3 commits into from
Apr 30, 2020
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 @@ -9,7 +9,10 @@
import com.microsoft.bot.builder.ActivityHandler;
import com.microsoft.bot.builder.InvokeResponse;
import com.microsoft.bot.builder.TurnContext;
import com.microsoft.bot.connector.rest.ErrorResponseException;
import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.Error;
import com.microsoft.bot.schema.ErrorResponse;
import com.microsoft.bot.schema.ResultPair;
import com.microsoft.bot.schema.Serialization;
import com.microsoft.bot.schema.teams.AppBasedLinkQuery;
Expand All @@ -30,10 +33,8 @@
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.stream.Collectors;

/**
* A Teams implementation of the Bot interface intended for further subclassing.
Expand Down Expand Up @@ -439,44 +440,46 @@ protected CompletableFuture<Void> onTeamsMembersAddedDispatch(
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();

Map<String, TeamsChannelAccount> teamMembers = null;

List<TeamsChannelAccount> teamsMembersAdded = new ArrayList<>();
for (ChannelAccount memberAdded : membersAdded) {
if (!memberAdded.getProperties().isEmpty()) {
// when the ChannelAccount object is fully a TeamsChannelAccount
// (when Teams changes the service to return the full details)
try {
JsonNode node = mapper.valueToTree(memberAdded);
teamsMembersAdded.add(mapper.treeToValue(node, TeamsChannelAccount.class));
} catch (JsonProcessingException jpe) {
return withException(jpe);
}
} else {
// this code path is intended to be temporary and should be removed in 4.7/4.8
// or whenever Teams is updated

// we have a simple ChannelAccount so will try to flesh out the details using
// the getMembers call
if (teamMembers == null) {
List<TeamsChannelAccount> result = TeamsInfo.getMembers(turnContext).join();
teamMembers = result.stream().collect(
Collectors.toMap(ChannelAccount::getId, item -> item)
);
TeamsChannelAccount teamsChannelAccount = null;
try {
teamsChannelAccount = TeamsInfo.getMember(turnContext, memberAdded.getId()).join();
} catch (CompletionException ex) {
Throwable causeException = ex.getCause();
if (causeException instanceof ErrorResponseException) {
ErrorResponse response = ((ErrorResponseException) causeException).body();
if (response != null) {
Error error = response.getError();
if (error != null && !error.getCode().equals("ConversationNotFound")) {
throw ex;
}
}
} else {
throw ex;
}
}

if (teamMembers.containsKey(memberAdded.getId())) {
teamsMembersAdded.add(teamMembers.get(memberAdded.getId()));
} else {
// unable to find the member added in ConversationUpdate Activity in the
// response from
// the getMembers call
TeamsChannelAccount newTeamsChannelAccount = new TeamsChannelAccount();
newTeamsChannelAccount.setId(memberAdded.getId());
newTeamsChannelAccount.setName(memberAdded.getName());
newTeamsChannelAccount.setAadObjectId(memberAdded.getAadObjectId());
newTeamsChannelAccount.setRole(memberAdded.getRole());

teamsMembersAdded.add(newTeamsChannelAccount);
if (teamsChannelAccount == null) {
// unable to find the member added in ConversationUpdate Activity in the response from the
// getMember call
teamsChannelAccount = new TeamsChannelAccount();
teamsChannelAccount.setId(memberAdded.getId());
teamsChannelAccount.setName(memberAdded.getName());
teamsChannelAccount.setAadObjectId(memberAdded.getAadObjectId());
teamsChannelAccount.setRole(memberAdded.getRole());
}
teamsMembersAdded.add(teamsChannelAccount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.microsoft.bot.connector.rest;

import com.microsoft.bot.azure.AzureResponseBuilder;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.AttachmentData;
import com.microsoft.bot.schema.ChannelAccount;
Expand Down Expand Up @@ -606,12 +607,13 @@ private ServiceResponse<ChannelAccount> getConversationMemberDelegate(
Response<ResponseBody> response
) throws ErrorResponseException, IOException, IllegalArgumentException {

return client.restClient()
return ((AzureResponseBuilder<ChannelAccount, ErrorResponseException>) client.restClient()
.responseBuilderFactory()
.<ChannelAccount, ErrorResponseException>newInstance(client.serializerAdapter())
.register(HttpURLConnection.HTTP_OK, new TypeToken<ChannelAccount>() {
}.getType())
.registerError(ErrorResponseException.class)
.registerError(ErrorResponseException.class))
.withThrowOnGet404(true)
.build(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,7 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
{
setTitle("Welcome Card");
setText("Click the buttons below to update this card");
setButtons(Arrays.asList(new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Update Card");
setText("UpdateCardAction");
setValue(value);
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Message All Members");
setText("MessageAllMembers");
}
}));
setButtons(getHeroCardButtons(value));
}
};

Expand Down Expand Up @@ -189,26 +176,7 @@ private CompletableFuture<Void> updateCardActivity(TurnContext turnContext) {
{
setTitle("Welcome Card");
setText("Update count - " + data.get("count"));
setButtons(Arrays.asList(new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Update Card");
setText("UpdateCardAction");
setValue(data);
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Message All Members");
setText("MessageAllMembers");
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Delete card");
setText("Delete");
}
}));
setButtons(getHeroCardButtons(data));
}
};

Expand All @@ -218,14 +186,43 @@ private CompletableFuture<Void> updateCardActivity(TurnContext turnContext) {
return turnContext.updateActivity(updatedActivity).thenApply(resourceResponse -> null);
}

private List<CardAction> getHeroCardButtons(Object value) {
return Arrays.asList(new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Update Card");
setText("UpdateCardAction");
setValue(value);
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Message All Members");
setText("MessageAllMembers");
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Delete card");
setText("Delete");
}
}, new CardAction() {
{
setType(ActionTypes.MESSAGE_BACK);
setTitle("Who am I?");
setText("MentionMe");
}
});
}

private CompletableFuture<Void> mentionActivity(TurnContext turnContext) {
Mention mention = new Mention();
mention.setMentioned(turnContext.getActivity().getFrom());
mention.setText(
"<at>" + URLEncoder.encode(turnContext.getActivity().getFrom().getName()) + "</at>"
);

Activity replyActivity = MessageFactory.text("Hello " + mention.getText() + ".'");
Activity replyActivity = MessageFactory.text("Hello " + mention.getText() + ".");
replyActivity.setMentions(Collections.singletonList(mention));

return turnContext.sendActivity(replyActivity).thenApply(resourceResponse -> null);
Expand Down