Exemple #1
0
        public static async Task <ResourceResponse> SendMessageToAgentAsReplyToConversationInAgentsChannel(
            IMessageActivity activity,
            string messageToSend,
            string agentConversationId,
            int vsoId)
        {
            var propertiesForLogging = new Dictionary <string, string>
            {
                { "function", "SendMessageToAgentAsReplyToConversationInAgentsChannel" },
                { "endUser", activity.From.Name },
                { "messageToSend", messageToSend },
                { "agentConversationId", agentConversationId },
                { "vsoId", vsoId.ToString() },
            };

            try
            {
                var isSms = IsPhoneNumber(activity.From.Name);

                ChannelAccount botAccount = isSms ? await IdTable.GetBotId() : activity.Recipient;

                var serviceUrl = TeamsServiceEndpoint;

                using (ConnectorClient connector = await BotConnectorUtility.BuildConnectorClientAsync(serviceUrl))
                {
                    IMessageActivity message = Activity.CreateMessageActivity();
                    message.From         = botAccount;
                    message.ReplyToId    = agentConversationId;
                    message.Conversation = new ConversationAccount
                    {
                        Id      = agentConversationId,
                        IsGroup = true,
                    };
                    IEnumerable <ChannelAccount> agentIds = await OnlineStatus.GetAgentIds();

                    var    channelAccounts = agentIds as ChannelAccount[] ?? agentIds.ToArray();
                    string atMentions      = Empty;
                    if (channelAccounts.Any())
                    {
                        //message.Entities = new List<Entity>(channelAccounts.Select(account =>
                        //    new Mention(account, "@" + account.Name.Replace(" ", "_"), "mention")));
                        atMentions = Join(" ", channelAccounts.Select(ca => "@" + ca.Name));
                    }

                    message.Text        = $"{atMentions} [{activity.From.Name}]: {messageToSend}";
                    message.TextFormat  = "plain";
                    message.ServiceUrl  = TeamsServiceEndpoint;
                    message.ChannelData = new Dictionary <string, object>
                    {
                        ["teamsChannelId"] = "19:[email protected]",
                        ["notification"]   = new Dictionary <string, object> {
                            { "alert", true }
                        }
                    };

                    ResourceResponse response = await BotConnectorUtility.BuildRetryPolicy().ExecuteAsync(async()
                                                                                                          => await connector.Conversations.SendToConversationAsync((Activity)message));

                    Trace.TraceInformation(
                        $"[SUCCESS]: SendMessageToAgentAsReplyToConversationInAgentsChannel. Message={messageToSend}. " +
                        $"response id ={response.Id} agentConversationId={agentConversationId} ");

                    propertiesForLogging.Add("replyMessageId", response.Id);
                    WebApiConfig.TelemetryClient.TrackEvent("SendMessageToAgentAsReplyToConversationInAgentsChannel",
                                                            propertiesForLogging);

                    return(response);
                }
            }
            catch (Exception e)
            {
                WebApiConfig.TelemetryClient.TrackException(e, propertiesForLogging);
                throw;
            }
        }