Exemple #1
0
        /// <summary>
        /// This is only here for reference purposes. Not used.
        /// </summary>
        /// <param name="endUserActivity"></param>
        /// <param name="messageToSend"></param>
        /// <param name="agentConversationId"></param>
        /// <returns></returns>
        public static async Task <ConversationResourceResponse> CreateAConversation(IMessageActivity endUserActivity,
                                                                                    string messageToSend,
                                                                                    string agentConversationId)
        {
            var botAccount = endUserActivity.Recipient;

            try
            {
                // To create a new reply chain
                var channelData = new Dictionary <string, object>
                {
                    ["teamsChannelId"] = "19:[email protected]",
                    ["notification"]   = new Dictionary <string, object>()
                    {
                        { "alert", true }
                    }
                };

                IMessageActivity agentMessage = Activity.CreateMessageActivity();
                agentMessage.From       = botAccount;
                agentMessage.Type       = ActivityTypes.Message;
                agentMessage.Text       = messageToSend;
                agentMessage.ChannelId  = MsTeamChannelId;
                agentMessage.ServiceUrl = endUserActivity.ServiceUrl;
                agentMessage.ReplyToId  = agentConversationId;

                var agentMessageActivity = (Activity)agentMessage;

                var conversationParams = new ConversationParameters()
                {
                    IsGroup     = true,
                    Bot         = botAccount,
                    Members     = null,
                    Activity    = agentMessageActivity,
                    ChannelData = channelData,
                };

                using (ConnectorClient connector =
                           await BotConnectorUtility.BuildConnectorClientAsync(endUserActivity.ServiceUrl))
                {
                    var createResponse = await BotConnectorUtility.BuildRetryPolicy().ExecuteAsync(async() =>
                                                                                                   await connector.Conversations.CreateConversationAsync(conversationParams));

                    return(createResponse);
                }
            }
            catch (Exception e)
            {
                WebApiConfig.TelemetryClient.TrackException(e, new Dictionary <string, string>
                {
                    { "function", "CreateAConversation" },
                    { "messageToSend", messageToSend },
                    { "agentConversationId", agentConversationId },
                });

                throw;
            }
        }
        public static async Task <ConversationResourceResponse> CreateAgentConversation(ChannelInfo targetChannelInfo,
                                                                                        AdaptiveCard card,
                                                                                        string topicName,
                                                                                        ConnectorClient connector,
                                                                                        int vsoTicketNumber,
                                                                                        IMessageActivity endUserActivity)
        {
            try
            {
                var channelData = new TeamsChannelData {
                    Channel = targetChannelInfo
                };

                IMessageActivity agentMessage = Activity.CreateMessageActivity();
                agentMessage.From       = endUserActivity.Recipient;
                agentMessage.Recipient  = new ChannelAccount(ConfigurationManager.AppSettings["AgentToAssignVsoTasksTo"]);
                agentMessage.Type       = ActivityTypes.Message;
                agentMessage.ChannelId  = ActivityHelper.MsTeamChannelId;
                agentMessage.ServiceUrl = endUserActivity.ServiceUrl;

                agentMessage.Attachments = new List <Attachment>
                {
                    new Attachment {
                        ContentType = AdaptiveCard.ContentType, Content = card
                    }
                };

                var agentMessageActivity = (Activity)agentMessage;

                ConversationParameters conversationParams = new ConversationParameters(
                    isGroup: true,
                    bot: null,
                    members: null,
                    topicName: topicName,
                    activity: agentMessageActivity,
                    channelData: channelData);

                var conversationResourceResponse = await BotConnectorUtility.BuildRetryPolicy().ExecuteAsync(async()
                                                                                                             => await connector.Conversations.CreateConversationAsync(conversationParams));

                Trace.TraceInformation($"[SUCCESS]: CreateAgentConversation. " +
                                       $"response id ={conversationResourceResponse.Id} vsoId={vsoTicketNumber} ");

                WebApiConfig.TelemetryClient.TrackEvent("CreateAgentConversation", new Dictionary <string, string>
                {
                    { "endUser", agentMessage.From.Name },
                    { "agentConversationId", conversationResourceResponse.Id },
                    { "vsoId", vsoTicketNumber.ToString() },
                });

                return(conversationResourceResponse);
            }
            catch (System.Exception e)
            {
                WebApiConfig.TelemetryClient.TrackException(e, new Dictionary <string, string>
                {
                    { "function", "CreateAgentConversation" },
                    { "endUser", endUserActivity.Recipient.Name },
                    { "vsoId", vsoTicketNumber.ToString() }
                });

                throw;
            }
        }
        public static async Task <string> CreateAgentConversationEx(IDialogContext context,
                                                                    string topicName,
                                                                    AdaptiveCard cardToSend,
                                                                    UserProfile endUserProfile)
        {
            string serviceUrl = GetServiceUrl(context);

            var agentChannelInfo = await IdTable.GetAgentChannelInfo();

            ChannelAccount botMsTeamsChannelAccount = context.Activity.ChannelId == ActivityHelper.SmsChannelId
                ? await IdTable.GetBotId()
                : context.Activity.From;

            using (var connectorClient = await BotConnectorUtility.BuildConnectorClientAsync(serviceUrl))
            {
                try
                {
                    var channelData = new TeamsChannelData {
                        Channel = agentChannelInfo
                    };

                    IMessageActivity agentMessage = Activity.CreateMessageActivity();
                    agentMessage.From      = botMsTeamsChannelAccount;
                    agentMessage.Recipient =
                        new ChannelAccount(ConfigurationManager.AppSettings["AgentToAssignVsoTasksTo"]);
                    agentMessage.Type       = ActivityTypes.Message;
                    agentMessage.ChannelId  = ActivityHelper.MsTeamChannelId;
                    agentMessage.ServiceUrl = serviceUrl;

                    agentMessage.Attachments = new List <Attachment>
                    {
                        new Attachment {
                            ContentType = AdaptiveCard.ContentType, Content = cardToSend
                        }
                    };

                    var agentMessageActivity = (Activity)agentMessage;

                    ConversationParameters conversationParams = new ConversationParameters(
                        isGroup: true,
                        bot: null,
                        members: null,
                        topicName: topicName,
                        activity: agentMessageActivity,
                        channelData: channelData);

                    var conversationResourceResponse = await BotConnectorUtility.BuildRetryPolicy().ExecuteAsync(
                        async()
                        => await connectorClient.Conversations.CreateConversationAsync(conversationParams));

                    Trace.TraceInformation(
                        $"[SUCCESS]: CreateAgentConversation. response id ={conversationResourceResponse.Id}");

                    WebApiConfig.TelemetryClient.TrackEvent("CreateAgentConversation", new Dictionary <string, string>
                    {
                        { "endUser", agentMessage.From.Name },
                        { "agentConversationId", conversationResourceResponse.Id },
                    });

                    return(conversationResourceResponse.Id);
                }
                catch (System.Exception e)
                {
                    WebApiConfig.TelemetryClient.TrackException(e, new Dictionary <string, string>
                    {
                        { "function", "CreateAgentConversation" }
                    });

                    throw;
                }
            }
        }
Exemple #4
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;
            }
        }
Exemple #5
0
        public static async Task <ResourceResponse> SendMessageToUserEx(IMessageActivity activity,
                                                                        string username,
                                                                        string userId,
                                                                        string messageToSend,
                                                                        string vsoId)
        {
            try
            {
                var userAccount = new ChannelAccount(userId, username);

                bool isSms = IsPhoneNumber(username);

                var serviceUrl = isSms ? SmsServiceEndpoint : activity.ServiceUrl;

                if (isSms)
                {
                    MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
                }

                var botPhoneNumber = ConfigurationManager.AppSettings["BotPhoneNumber"];
                var botAccount     = isSms
                    ? new ChannelAccount(botPhoneNumber, botPhoneNumber)
                    : activity.Recipient;

                using (ConnectorClient connector = await BotConnectorUtility.BuildConnectorClientAsync(serviceUrl))
                {
                    var conversation = connector.Conversations.CreateOrGetDirectConversation(botAccount,
                                                                                             userAccount, activity.GetChannelData <TeamsChannelData>().Tenant.Id);

                    IMessageActivity message = Activity.CreateMessageActivity();
                    message.From         = botAccount;
                    message.Recipient    = userAccount;
                    message.Conversation = new ConversationAccount(id: conversation.Id);
                    message.Text         = $"[Mary (human)] {messageToSend}";
                    message.TextFormat   = "plain";
                    message.Locale       = "en-Us";
                    message.ChannelId    = isSms ? SmsChannelId : MsTeamChannelId;
                    message.ServiceUrl   = serviceUrl;

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

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

                    WebApiConfig.TelemetryClient.TrackEvent("SendMessageToUserEx", new Dictionary <string, string>
                    {
                        { "endUser", username },
                        { "messageToSend", messageToSend },
                        { "endUserConversationId", response.Id },
                        { "vsoId", vsoId },
                    });

                    return(response);
                }
            }
            catch (Exception e)
            {
                WebApiConfig.TelemetryClient.TrackException(e, new Dictionary <string, string>
                {
                    { "function", "SendMessageToUserEx" },
                    { "endUser", username },
                    { "messageToSend", messageToSend },
                    { "vsoId", vsoId },
                });
                throw;
            }
        }