예제 #1
0
        public UserTable()
        {
            try
            {
                // Retrieve the storage account from the connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

                // Create the IdTableClient client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                // Create the CloudTable object that represents the "people" IdTableClient.
                UserTableClient = tableClient.GetTableReference(UserTableName);

                _uniqueIdGenerator = new UniqueIdGenerator(new BlobOptimisticDataStore(storageAccount, "UniqueIdGenerator"));

                _retryPolicy = BotConnectorUtility.BuildRetryPolicy();
            }
            catch (System.Exception e)
            {
                WebApiConfig.TelemetryClient.TrackException(e, new Dictionary <string, string>
                {
                    { "debugNote", "failed to init OnlineStatus table client" },
                });
                throw;
            }
        }
예제 #2
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 PostWithRetryAsync(this IDialogContext context, IMessageActivity activity)
 {
     var retryPolicy = BotConnectorUtility.BuildRetryPolicy();
     await retryPolicy.ExecuteAsync(async() => await context.PostAsync(activity));
 }
 public static async Task PostWithRetryAsync(this IDialogContext context, string text)
 {
     var retryPolicy = BotConnectorUtility.BuildRetryPolicy();
     await retryPolicy.ExecuteAsync(async() => await context.PostAsync(text));
 }
예제 #5
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;
            }
        }
예제 #6
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;
            }
        }