@@ -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.
0 commit comments