@@ -219,15 +219,7 @@ public async Task<bool> DeleteConversationMessage([FromRoute] string conversatio
219
219
return response ;
220
220
}
221
221
222
- private void SetStates ( IConversationService conv , NewMessageModel input )
223
- {
224
- conv . States . SetState ( "channel" , input . Channel , source : StateSource . External )
225
- . SetState ( "provider" , input . Provider , source : StateSource . External )
226
- . SetState ( "model" , input . Model , source : StateSource . External )
227
- . SetState ( "temperature" , input . Temperature , source : StateSource . External )
228
- . SetState ( "sampling_factor" , input . SamplingFactor , source : StateSource . External ) ;
229
- }
230
-
222
+ #region Send message
231
223
[ HttpPost ( "/conversation/{agentId}/{conversationId}" ) ]
232
224
public async Task < ChatResponseModel > SendMessage ( [ FromRoute ] string agentId ,
233
225
[ FromRoute ] string conversationId ,
@@ -350,6 +342,73 @@ await conv.SendMessage(agentId, inputMsg,
350
342
351
343
// await OnEventCompleted(Response);
352
344
}
345
+ #endregion
346
+
347
+ #region Files and attachments
348
+ [ HttpPost ( "/conversation/{conversationId}/attachments" ) ]
349
+ public IActionResult UploadAttachments ( [ FromRoute ] string conversationId ,
350
+ IFormFile [ ] files )
351
+ {
352
+ if ( files != null && files . Length > 0 )
353
+ {
354
+ var fileService = _services . GetRequiredService < IBotSharpFileService > ( ) ;
355
+ var dir = fileService . GetDirectory ( conversationId ) ;
356
+ foreach ( var file in files )
357
+ {
358
+ // Save the file, process it, etc.
359
+ var fileName = ContentDispositionHeaderValue . Parse ( file . ContentDisposition ) . FileName . Trim ( '"' ) ;
360
+ var filePath = Path . Combine ( dir , fileName ) ;
361
+
362
+ using ( var stream = new FileStream ( filePath , FileMode . Create ) )
363
+ {
364
+ file . CopyTo ( stream ) ;
365
+ }
366
+ }
367
+
368
+ return Ok ( new { message = "File uploaded successfully." } ) ;
369
+ }
370
+
371
+ return BadRequest ( new { message = "Invalid file." } ) ;
372
+ }
373
+
374
+ [ HttpGet ( "/conversation/{conversationId}/files/{messageId}" ) ]
375
+ public IEnumerable < MessageFileViewModel > GetMessageFiles ( [ FromRoute ] string conversationId , [ FromRoute ] string messageId )
376
+ {
377
+ var fileService = _services . GetRequiredService < IBotSharpFileService > ( ) ;
378
+ var files = fileService . GetMessageFiles ( conversationId , new List < string > { messageId } ) ;
379
+ return files ? . Select ( x => MessageFileViewModel . Transform ( x ) ) ? . ToList ( ) ?? new List < MessageFileViewModel > ( ) ;
380
+ }
381
+
382
+ [ HttpGet ( "/conversation/{conversationId}/message/{messageId}/file/{fileName}" ) ]
383
+ public IActionResult GetMessageFile ( [ FromRoute ] string conversationId , [ FromRoute ] string messageId , [ FromRoute ] string fileName )
384
+ {
385
+ var fileService = _services . GetRequiredService < IBotSharpFileService > ( ) ;
386
+ var file = fileService . GetMessageFile ( conversationId , messageId , fileName ) ;
387
+ if ( string . IsNullOrEmpty ( file ) )
388
+ {
389
+ return NotFound ( ) ;
390
+ }
391
+ return BuildFileResult ( file ) ;
392
+ }
393
+ #endregion
394
+
395
+ #region Private methods
396
+ private void SetStates ( IConversationService conv , NewMessageModel input )
397
+ {
398
+ conv . States . SetState ( "channel" , input . Channel , source : StateSource . External )
399
+ . SetState ( "provider" , input . Provider , source : StateSource . External )
400
+ . SetState ( "model" , input . Model , source : StateSource . External )
401
+ . SetState ( "temperature" , input . Temperature , source : StateSource . External )
402
+ . SetState ( "sampling_factor" , input . SamplingFactor , source : StateSource . External ) ;
403
+ }
404
+
405
+ private FileContentResult BuildFileResult ( string file )
406
+ {
407
+ using Stream stream = System . IO . File . Open ( file , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
408
+ var bytes = new byte [ stream . Length ] ;
409
+ stream . Read ( bytes , 0 , ( int ) stream . Length ) ;
410
+ return File ( bytes , "application/octet-stream" , Path . GetFileName ( file ) ) ;
411
+ }
353
412
354
413
private async Task OnChunkReceived ( HttpResponse response , ChatResponseModel message )
355
414
{
@@ -391,4 +450,5 @@ private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
391
450
392
451
return jsonOption ;
393
452
}
453
+ #endregion
394
454
}
0 commit comments