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

MessagesController.cs 2.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  1. namespace EmergencyServicesBot
  2. {
  3. using System;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using System.Web.Http;
  8. using Microsoft.Bot.Connector;
  9. [BotAuthentication]
  10. public class MessagesController : ApiController
  11. {
  12. /// <summary>
  13. /// POST: api/Messages
  14. /// Receive a message from a user and reply to it
  15. /// </summary>
  16. public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
  17. {
  18. if (activity.Type == ActivityTypes.Message)
  19. {
  20. ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
  21. // calculate something for us to return
  22. int length = (activity.Text ?? string.Empty).Length;
  23. // return our reply to the user
  24. Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
  25. await connector.Conversations.ReplyToActivityAsync(reply);
  26. }
  27. else
  28. {
  29. this.HandleSystemMessage(activity);
  30. }
  31. var response = Request.CreateResponse(HttpStatusCode.OK);
  32. return response;
  33. }
  34. private Activity HandleSystemMessage(Activity message)
  35. {
  36. if (message.Type == ActivityTypes.DeleteUserData)
  37. {
  38. // Implement user deletion here
  39. // If we handle user deletion, return a real message
  40. }
  41. else if (message.Type == ActivityTypes.ConversationUpdate)
  42. {
  43. // Handle conversation state changes, like members being added and removed
  44. // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
  45. // Not available in all channels
  46. }
  47. else if (message.Type == ActivityTypes.ContactRelationUpdate)
  48. {
  49. // Handle add/remove from contact lists
  50. // Activity.From + Activity.Action represent what happened
  51. }
  52. else if (message.Type == ActivityTypes.Typing)
  53. {
  54. // Handle knowing tha the user is typing
  55. }
  56. else if (message.Type == ActivityTypes.Ping)
  57. {
  58. }
  59. return null;
  60. }
  61. }
  62. }
Tip!

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

Comments

Loading...