Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ReceiveAttachmentDialog.cs 2.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  1. namespace ReceiveAttachmentBot
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Threading.Tasks;
  9. using Microsoft.Bot.Builder.Dialogs;
  10. using Microsoft.Bot.Connector;
  11. [Serializable]
  12. internal class ReceiveAttachmentDialog : IDialog<object>
  13. {
  14. public async Task StartAsync(IDialogContext context)
  15. {
  16. context.Wait(this.MessageReceivedAsync);
  17. }
  18. public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
  19. {
  20. var message = await argument;
  21. if (message.Attachments != null && message.Attachments.Any())
  22. {
  23. var attachment = message.Attachments.First();
  24. using (HttpClient httpClient = new HttpClient())
  25. {
  26. // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
  27. if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
  28. && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
  29. {
  30. var token = await new MicrosoftAppCredentials().GetTokenAsync();
  31. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  32. }
  33. var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
  34. var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
  35. await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
  36. }
  37. }
  38. else
  39. {
  40. await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
  41. }
  42. context.Wait(this.MessageReceivedAsync);
  43. }
  44. }
  45. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...