1818using HtmlAgilityPack ;
1919using Services ;
2020using Models ;
21+ using System . IO ;
2122
2223
2324namespace Microsoft . BotBuilderSamples
@@ -77,13 +78,23 @@ public override async Task<List<string>> ProcessMessage(ConversationData convers
7778 // await turnContext.SendActivityAsync($"Thread found: {conversationData.ThreadId}");
7879 }
7980
81+ // Process any attachments
82+
83+ if ( ! turnContext . Activity . Attachments . IsNullOrEmpty ( ) )
84+ foreach ( Bot . Schema . Attachment attachment in turnContext . Activity . Attachments )
85+ await IngestAttachment ( conversationData , turnContext , attachment ) ;
86+
87+ if ( turnContext . Activity . Text . IsNullOrEmpty ( ) )
88+ return new List < string > ( ) { "1" } ;
89+
90+ // Process keywords
8091 if ( turnContext . Activity . Text . ToLower ( ) == "clear" )
8192 {
8293 var thread = await _aoaiClient . DeleteThread ( conversationData . ThreadId ) ;
8394 conversationData . ThreadId = null ;
8495 conversationData . History . Clear ( ) ;
8596 conversationData . Attachments . Clear ( ) ;
86- return new List < string > { $ "Thread { thread . Id } deleted." } ;
97+ return new List < string > ( ) { $ "Thread { thread . Id } deleted." } ;
8798 }
8899
89100 // Add user message to thread
@@ -113,7 +124,8 @@ public override async Task<List<string>> ProcessMessage(ConversationData convers
113124 System . Threading . Thread . Sleep ( 10000 ) ;
114125 run = await _aoaiClient . GetThreadRun ( conversationData . ThreadId , run . Id ) ;
115126 }
116- if ( run . Status == "failed" ) {
127+ if ( run . Status == "failed" )
128+ {
117129 await turnContext . SendActivityAsync ( "Something went wrong when running the assistant." ) ;
118130 }
119131
@@ -134,7 +146,7 @@ public override async Task<List<string>> ProcessMessage(ConversationData convers
134146 if ( messages [ i ] . Content [ j ] . Type == "image_file" )
135147 {
136148 responses . Add ( $ "Image (ID: { messages [ i ] . Content [ j ] . ImageFile . FileId } )") ;
137- List < object > images = [ new { type = "Image" , url = $ "{ _appUrl } /openai/files/{ messages [ i ] . Content [ j ] . ImageFile . FileId } /content" } ] ;
149+ List < object > images = new ( ) { new { type = "Image" , url = $ "{ _appUrl } /openai/files/{ messages [ i ] . Content [ j ] . ImageFile . FileId } /content" } } ;
138150 object adaptiveCardJson = new
139151 {
140152 type = "AdaptiveCard" ,
@@ -153,5 +165,22 @@ public override async Task<List<string>> ProcessMessage(ConversationData convers
153165 }
154166 return responses ;
155167 }
168+
169+
170+ private async Task IngestAttachment ( ConversationData conversationData , ITurnContext < IMessageActivity > turnContext , Bot . Schema . Attachment attachment )
171+ {
172+ Uri fileUri = new Uri ( attachment . ContentUrl ) ;
173+ var httpClient = new HttpClient ( ) ;
174+ var stream = await httpClient . GetStreamAsync ( fileUri ) ;
175+ var file = await _aoaiClient . UploadFile ( stream , attachment . Name ) ;
176+ await _aoaiClient . SendMessage ( conversationData . ThreadId , new MessageInput ( )
177+ {
178+ Role = "user" ,
179+ Content = "(File uploaded)" ,
180+ FileIds = new List < string > ( ) { file . Id }
181+ } ) ;
182+ stream . Dispose ( ) ;
183+ await turnContext . SendActivityAsync ( $ "File { attachment . Name } uploaded successfully!") ;
184+ }
156185 }
157186}
0 commit comments