-
Notifications
You must be signed in to change notification settings - Fork 177
/
ZoomRequestMapper.cs
135 lines (118 loc) · 5.67 KB
/
ZoomRequestMapper.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Collections.Generic;
using Bot.Builder.Community.Adapters.Zoom.Models;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Linq;
using Bot.Builder.Community.Adapters.Zoom.Attachments;
using Bot.Builder.Community.Adapters.Shared.Attachments;
namespace Bot.Builder.Community.Adapters.Zoom
{
public class ZoomRequestMapper
{
private static readonly AttachmentConverter _attachmentConverter = DefaultZoomAttachmentConverter.CreateDefault();
private readonly ZoomRequestMapperOptions _options;
private readonly ILogger _logger;
public ZoomRequestMapper(ZoomRequestMapperOptions options, ILogger logger)
{
_options = options;
_logger = logger ?? NullLogger.Instance;
}
public Activity RequestToActivity(ZoomRequest request)
{
var activity = new Activity();
activity = SetGeneralActivityProperties(activity, request);
switch (request.Event)
{
case "bot_notification":
var botNotificationPayload = request.Payload.ToObject<BotNotificationPayload>();
activity.Type = ActivityTypes.Message;
activity.Text = botNotificationPayload.Cmd;
break;
case "interactive_message_actions":
var messageActionsPayload = request.Payload.ToObject<InteractiveMessageActionsPayload>();
activity.Type = ActivityTypes.Message;
activity.Text = messageActionsPayload.ActionItem.Text;
break;
case "interactive_message_fields_editable":
var fieldsEditablePayload = request.Payload.ToObject<InteractiveMessageFieldsEditablePayload>();
activity.Value = fieldsEditablePayload;
activity.Type = ActivityTypes.Event;
activity.Name = request.Event;
break;
case "interactive_message_select":
var selectPayload = request.Payload.ToObject<InteractiveMessageSelectPayload>();
activity.Value = selectPayload;
activity.Type = ActivityTypes.Event;
activity.Name = request.Event;
break;
default:
activity.Type = ActivityTypes.Event;
activity.Name = request.Event;
activity.Value = request.Payload;
break;
}
return activity;
}
private Activity SetGeneralActivityProperties(Activity activity, ZoomRequest request)
{
var botNotificationPayload = request.Payload.ToObject<Payload>();
activity.Conversation = new ConversationAccount();
activity.From = new ChannelAccount(botNotificationPayload.UserJid, botNotificationPayload.UserName);
activity.ChannelData = request;
activity.Recipient = new ChannelAccount(botNotificationPayload.ToJid);
activity.ChannelId = "zoom";
activity.Conversation = new ConversationAccount(id: $"{botNotificationPayload.AccountId}:{botNotificationPayload.ChannelName}");
return activity;
}
public ChatResponse ActivityToZoom(Activity activity)
{
var message = new ChatResponse()
{
ToJid = activity.From.Id,
AccountId = activity.Conversation.Id.Split(':')[0],
RobotJid = _options.RobotJid,
Content = new ChatResponseContent()
{
Head = !string.IsNullOrEmpty(activity.Text) ? new Head() { Text = $"{activity.Text}" } : null,
Body = new List<BodyItem>()
}
};
if (activity.SuggestedActions?.Actions != null && activity.SuggestedActions.Actions.Any())
{
var actions = new List<ZoomAction>();
foreach (var suggestedAction in activity.SuggestedActions.Actions)
{
actions.Add(new ZoomAction()
{
Style = "Default",
Text = suggestedAction.DisplayText,
Value = suggestedAction.Text
});
}
message.Content.Body.Add(new ActionsItem()
{
Actions = actions
});
}
ProcessActivityAttachments(activity, message);
return message;
}
private void ProcessActivityAttachments(Activity activity, ChatResponse response)
{
_attachmentConverter.ConvertAttachments(activity);
ProcessAttachment<MessageBodyItemWithLink>(ZoomAttachmentContentTypes.MessageWithLink, activity, response);
ProcessAttachment<FieldsBodyItem>(ZoomAttachmentContentTypes.Fields, activity, response);
ProcessAttachment<AttachmentBodyItem>(ZoomAttachmentContentTypes.Attachment, activity, response);
ProcessAttachment<DropdownBodyItem>(ZoomAttachmentContentTypes.Dropdown, activity, response);
}
private static void ProcessAttachment<T>(string contentType, Activity activity, ChatResponse response) where T : BodyItem
{
var messageWithLinks = activity.Attachments?
.Where(a => a.ContentType == contentType)
.Select(a => (T)a.Content).ToList();
if (messageWithLinks != null && messageWithLinks.Any())
response.Content.Body.AddRange(messageWithLinks);
}
}
}