Skip to content

Commit ba84368

Browse files
author
Eric Dahlvang
authored
Remove usage of CreateReply method from all samples. (#1575)
1 parent f2c61a1 commit ba84368

File tree

8 files changed

+63
-75
lines changed

8 files changed

+63
-75
lines changed

samples/csharp_dotnetcore/03.welcome-user/Bots/WelcomeUserBot.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
106106

107107
private static async Task SendIntroCardAsync(ITurnContext turnContext, CancellationToken cancellationToken)
108108
{
109-
var response = turnContext.Activity.CreateReply();
110-
111109
var card = new HeroCard();
112110
card.Title = "Welcome to Bot Framework!";
113111
card.Text = @"Welcome to Welcome Users bot sample! This Introduction card
@@ -121,8 +119,8 @@ some things to get them started. We use this opportunity to
121119
new CardAction(ActionTypes.OpenUrl, "Ask a question", null, "Ask a question", "Ask a question", "https://stackoverflow.com/questions/tagged/botframework"),
122120
new CardAction(ActionTypes.OpenUrl, "Learn how to deploy", null, "Learn how to deploy", "Learn how to deploy", "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0"),
123121
};
124-
125-
response.Attachments = new List<Attachment>() { card.ToAttachment() };
122+
123+
var response = MessageFactory.Attachment(card.ToAttachment());
126124
await turnContext.SendActivityAsync(response, cancellationToken);
127125
}
128126
}

samples/csharp_dotnetcore/06.using-cards/Dialogs/MainDialog.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ private async Task<DialogTurnResult> ChoiceCardStepAsync(WaterfallStepContext st
5757
private async Task<DialogTurnResult> ShowCardStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
5858
{
5959
_logger.LogInformation("MainDialog.ShowCardStepAsync");
60-
61-
// Reply to the activity we received with an activity.
62-
var reply = stepContext.Context.Activity.CreateReply();
63-
60+
6461
// Cards are sent as Attachments in the Bot Framework.
65-
// So we need to create a list of attachments on the activity.
66-
reply.Attachments = new List<Attachment>();
67-
62+
// So we need to create a list of attachments for the reply activity.
63+
var attachments = new List<Attachment>();
64+
65+
// Reply to the activity we received with an activity.
66+
var reply = MessageFactory.Attachment(attachments);
67+
6868
// Decide which type of card(s) we are going to show the user
6969
switch (((FoundChoice)stepContext.Result).Value)
7070
{

samples/csharp_dotnetcore/08.suggested-actions/Bots/SuggestedActionsBot.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ private static string ProcessInput(string text)
8686
/// "ActionTypes" that may be used for different situations.
8787
private static async Task SendSuggestedActionsAsync(ITurnContext turnContext, CancellationToken cancellationToken)
8888
{
89-
var reply = turnContext.Activity.CreateReply("What is your favorite color?");
89+
var reply = MessageFactory.Text("What is your favorite color?");
90+
9091
reply.SuggestedActions = new SuggestedActions()
9192
{
9293
Actions = new List<CardAction>()

samples/csharp_dotnetcore/15.handling-attachments/Bots/AttachmentsBot.cs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
3838

3939
private static async Task DisplayOptionsAsync(ITurnContext turnContext, CancellationToken cancellationToken)
4040
{
41-
var reply = turnContext.Activity.CreateReply();
42-
4341
// Create a HeroCard with options for the user to interact with the bot.
4442
var card = new HeroCard
4543
{
@@ -55,9 +53,7 @@ private static async Task DisplayOptionsAsync(ITurnContext turnContext, Cancella
5553
},
5654
};
5755

58-
// Add the card to our reply.
59-
reply.Attachments = new List<Attachment>() { card.ToAttachment() };
60-
56+
var reply = MessageFactory.Attachment(card.ToAttachment());
6157
await turnContext.SendActivityAsync(reply, cancellationToken);
6258
}
6359

@@ -79,53 +75,56 @@ await turnContext.SendActivityAsync(
7975
}
8076

8177
// Given the input from the message, create the response.
82-
private static Activity ProcessInput(ITurnContext turnContext)
78+
private static IMessageActivity ProcessInput(ITurnContext turnContext)
8379
{
8480
var activity = turnContext.Activity;
85-
var reply = activity.CreateReply();
81+
IMessageActivity reply = null;
8682

8783
if (activity.Attachments != null && activity.Attachments.Any())
8884
{
8985
// We know the user is sending an attachment as there is at least one item
9086
// in the Attachments list.
91-
HandleIncomingAttachment(activity, reply);
87+
reply = HandleIncomingAttachment(activity);
9288
}
9389
else
9490
{
9591
// Send at attachment to the user.
96-
HandleOutgoingAttachment(activity, reply);
92+
reply = HandleOutgoingAttachment(turnContext, activity);
9793
}
9894

9995
return reply;
10096
}
10197

102-
// Adds an attachment to the 'reply' parameter that is passed in.
103-
private static void HandleOutgoingAttachment(IMessageActivity activity, IMessageActivity reply)
98+
// Returns a reply with the requested Attachment
99+
private static IMessageActivity HandleOutgoingAttachment(ITurnContext turnContext, IMessageActivity activity)
104100
{
105101
// Look at the user input, and figure out what kind of attachment to send.
102+
IMessageActivity reply = null;
106103
if (activity.Text.StartsWith("1"))
107104
{
108-
reply.Text = "This is an inline attachment.";
105+
reply = MessageFactory.Text("This is an inline attachment.");
109106
reply.Attachments = new List<Attachment>() { GetInlineAttachment() };
110107
}
111108
else if (activity.Text.StartsWith("2"))
112109
{
113-
reply.Text = "This is an attachment from a HTTP URL.";
110+
reply = MessageFactory.Text("This is an attachment from a HTTP URL.");
114111
reply.Attachments = new List<Attachment>() { GetInternetAttachment() };
115112
}
116113
else if (activity.Text.StartsWith("3"))
117114
{
118-
reply.Text = "This is an uploaded attachment.";
115+
reply = MessageFactory.Text("This is an uploaded attachment.");
119116

120117
// Get the uploaded attachment.
121-
var uploadedAttachment = GetUploadedAttachmentAsync(reply.ServiceUrl, reply.Conversation.Id).Result;
118+
var uploadedAttachment = GetUploadedAttachmentAsync(turnContext, activity.ServiceUrl, activity.Conversation.Id).Result;
122119
reply.Attachments = new List<Attachment>() { uploadedAttachment };
123120
}
124121
else
125122
{
126123
// The user did not enter input that this bot was built to handle.
127-
reply.Text = "Your input was not recognized please try again.";
124+
reply = MessageFactory.Text("Your input was not recognized please try again.");
128125
}
126+
127+
return reply;
129128
}
130129

131130

@@ -135,8 +134,9 @@ private static void HandleOutgoingAttachment(IMessageActivity activity, IMessage
135134
// on file type, size, and other attributes. Consult the documentation for the channel for
136135
// more information. For example Skype's limits are here
137136
// <see ref="https://support.skype.com/en/faq/FA34644/skype-file-sharing-file-types-size-and-time-limits"/>.
138-
private static void HandleIncomingAttachment(IMessageActivity activity, IMessageActivity reply)
137+
private static IMessageActivity HandleIncomingAttachment(IMessageActivity activity)
139138
{
139+
string replyText = string.Empty;
140140
foreach (var file in activity.Attachments)
141141
{
142142
// Determine where the file is hosted.
@@ -151,9 +151,11 @@ private static void HandleIncomingAttachment(IMessageActivity activity, IMessage
151151
webClient.DownloadFile(remoteFileUrl, localFileName);
152152
}
153153

154-
reply.Text = $"Attachment \"{activity.Attachments[0].Name}\"" +
155-
$" has been received and saved to \"{localFileName}\"";
154+
replyText += $"Attachment \"{file.Name}\"" +
155+
$" has been received and saved to \"{localFileName}\"\r\n";
156156
}
157+
158+
return MessageFactory.Text(replyText);
157159
}
158160

159161

@@ -176,7 +178,7 @@ private static Attachment GetInlineAttachment()
176178
}
177179

178180
// Creates an "Attachment" to be sent from the bot to the user from an uploaded file.
179-
private static async Task<Attachment> GetUploadedAttachmentAsync(string serviceUrl, string conversationId)
181+
private static async Task<Attachment> GetUploadedAttachmentAsync(ITurnContext turnContext, string serviceUrl, string conversationId)
180182
{
181183
if (string.IsNullOrWhiteSpace(serviceUrl))
182184
{
@@ -190,28 +192,25 @@ private static async Task<Attachment> GetUploadedAttachmentAsync(string serviceU
190192

191193
var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\architecture-resize.png");
192194

193-
// Create a connector client to use to upload the image.
194-
using (var connector = new ConnectorClient(new Uri(serviceUrl)))
195-
{
196-
var attachments = new Attachments(connector);
197-
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
198-
conversationId,
199-
new AttachmentData
200-
{
201-
Name = @"Resources\architecture-resize.png",
202-
OriginalBase64 = File.ReadAllBytes(imagePath),
203-
Type = "image/png",
204-
});
205-
206-
var attachmentUri = attachments.GetAttachmentUri(response.Id);
207-
208-
return new Attachment
195+
var connector = turnContext.TurnState.Get<IConnectorClient>() as ConnectorClient;
196+
var attachments = new Attachments(connector);
197+
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
198+
conversationId,
199+
new AttachmentData
209200
{
210201
Name = @"Resources\architecture-resize.png",
211-
ContentType = "image/png",
212-
ContentUrl = attachmentUri,
213-
};
214-
}
202+
OriginalBase64 = File.ReadAllBytes(imagePath),
203+
Type = "image/png",
204+
});
205+
206+
var attachmentUri = attachments.GetAttachmentUri(response.Id);
207+
208+
return new Attachment
209+
{
210+
Name = @"Resources\architecture-resize.png",
211+
ContentType = "image/png",
212+
ContentUrl = attachmentUri,
213+
};
215214
}
216215

217216
// Creates an <see cref="Attachment"/> to be sent from the bot to the user from a HTTP URL.

samples/csharp_dotnetcore/16.proactive-messages/Bots/ProactiveBot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersA
4343
// Greet anyone that was not the target (recipient) of this message.
4444
if (member.Id != turnContext.Activity.Recipient.Id)
4545
{
46-
await turnContext.SendActivityAsync(((Activity)turnContext.Activity).CreateReply(WelcomeMessage), cancellationToken);
46+
await turnContext.SendActivityAsync(MessageFactory.Text(WelcomeMessage), cancellationToken);
4747
}
4848
}
4949
}
@@ -53,7 +53,7 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
5353
AddConversationReference(turnContext.Activity as Activity);
5454

5555
// Echo back what the user said
56-
await turnContext.SendActivityAsync(((Activity)turnContext.Activity).CreateReply($"You sent '{turnContext.Activity.Text}'"), cancellationToken);
56+
await turnContext.SendActivityAsync(MessageFactory.Text($"You sent '{turnContext.Activity.Text}'"), cancellationToken);
5757
}
5858
}
5959
}

samples/csharp_dotnetcore/17.multilingual-bot/Bots/MultiLingualBot.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
5555
// selected language.
5656
// If Spanish was selected by the user, the reply below will actually be shown in spanish to the user.
5757
await _languagePreference.SetAsync(turnContext, lang, cancellationToken);
58-
var reply = ((Activity)turnContext.Activity).CreateReply($"Your current language code is: {lang}");
59-
58+
var reply = MessageFactory.Text($"Your current language code is: {lang}");
6059
await turnContext.SendActivityAsync(reply, cancellationToken);
6160

6261
// Save the user profile updates into the user state.
@@ -67,7 +66,7 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
6766
// Show the user the possible options for language. If the user chooses a different language
6867
// than the default, then the translation middleware will pick it up from the user state and
6968
// translate messages both ways, i.e. user to bot and bot to user.
70-
var reply = ((Activity)turnContext.Activity).CreateReply("Choose your language:");
69+
var reply = MessageFactory.Text("Choose your language:");
7170
reply.SuggestedActions = new SuggestedActions()
7271
{
7372
Actions = new List<CardAction>()
@@ -90,21 +89,13 @@ private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, Canc
9089
if (member.Id != turnContext.Activity.Recipient.Id)
9190
{
9291
var welcomeCard = CreateAdaptiveCardAttachment();
93-
var response = CreateResponse(turnContext.Activity, welcomeCard);
92+
var response = MessageFactory.Attachment(welcomeCard);
9493
await turnContext.SendActivityAsync(response, cancellationToken);
95-
await turnContext.SendActivityAsync(turnContext.Activity.CreateReply(WelcomeText), cancellationToken);
94+
await turnContext.SendActivityAsync(MessageFactory.Text(WelcomeText), cancellationToken);
9695
}
9796
}
9897
}
9998

100-
// Create an attachment message response.
101-
private static Activity CreateResponse(IActivity activity, Attachment attachment)
102-
{
103-
var response = ((Activity)activity).CreateReply();
104-
response.Attachments = new List<Attachment>() { attachment };
105-
return response;
106-
}
107-
10899
// Load attachment from file.
109100
private static Attachment CreateAdaptiveCardAttachment()
110101
{

samples/csharp_dotnetcore/23.facebook-events/Bots/FacebookBot.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected virtual async Task OnFacebookPostBack(ITurnContext turnContext, Facebo
135135
// TODO: Your PostBack handling logic here...
136136

137137
// Answer the postback, and show choices
138-
var reply = turnContext.Activity.CreateReply($"Are you sure?");
138+
var reply = MessageFactory.Text("Are you sure?");
139139
await turnContext.SendActivityAsync(reply, cancellationToken);
140140
await ShowChoices(turnContext, cancellationToken);
141141
}
@@ -156,7 +156,7 @@ protected virtual async Task OnFacebookQuickReply(ITurnContext turnContext, Face
156156
// The Facebook page id from which the message comes from is in turnContext.Activity.Recipient.Id.
157157
case FacebookPageIdOption:
158158
{
159-
var reply = turnContext.Activity.CreateReply($"This message comes from the following Facebook Page: {turnContext.Activity.Recipient.Id}");
159+
var reply = MessageFactory.Text($"This message comes from the following Facebook Page: {turnContext.Activity.Recipient.Id}");
160160
await turnContext.SendActivityAsync(reply, cancellationToken);
161161
await ShowChoices(turnContext, cancellationToken);
162162

@@ -175,9 +175,8 @@ protected virtual async Task OnFacebookQuickReply(ITurnContext turnContext, Face
175175
new CardAction() { Title = "No", Type = ActionTypes.PostBack, Value = "No" },
176176
},
177177
};
178-
179-
var reply = turnContext.Activity.CreateReply();
180-
reply.Attachments = new List<Attachment> { card.ToAttachment() };
178+
179+
var reply = MessageFactory.Attachment(card.ToAttachment());
181180
await turnContext.SendActivityAsync(reply, cancellationToken);
182181

183182
break;

samples/csharp_dotnetcore/24.bot-authentication-msgraph/OAuthHelpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static async Task ListRecentMailAsync(ITurnContext turnContext, TokenResp
7878

7979
var client = new SimpleGraphClient(tokenResponse.Token);
8080
var messages = await client.GetRecentMailAsync();
81-
var reply = turnContext.Activity.CreateReply();
81+
IMessageActivity reply = null;
8282

8383
if (messages.Any())
8484
{
@@ -88,7 +88,7 @@ public static async Task ListRecentMailAsync(ITurnContext turnContext, TokenResp
8888
count = 5;
8989
}
9090

91-
reply.Attachments = new List<Attachment>();
91+
reply = MessageFactory.Attachment(new List<Attachment>());
9292
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
9393

9494
for (var i = 0; i < count; i++)
@@ -109,7 +109,7 @@ public static async Task ListRecentMailAsync(ITurnContext turnContext, TokenResp
109109
}
110110
else
111111
{
112-
reply.Text = "Unable to find any recent unread mail.";
112+
reply = MessageFactory.Text("Unable to find any recent unread mail.");
113113
}
114114

115115
await turnContext.SendActivityAsync(reply);

0 commit comments

Comments
 (0)