/// <summary>
        /// Tries to send the given message activity to the given party using this bot on the same
        /// channel as the party who the message is sent to.
        /// </summary>
        /// <param name="partyToMessage">The party to send the message to.</param>
        /// <param name="messageActivity">The message activity to send (message content).</param>
        /// <returns>The ResourceResponse instance or null in case of an error.</returns>
        public async Task <ResourceResponse> SendMessageToPartyByBotAsync(Party partyToMessage, IMessageActivity messageActivity)
        {
            Party botParty = null;

            if (partyToMessage != null)
            {
                // We need the channel account of the bot in the SAME CHANNEL as the RECIPIENT.
                // The identity of the bot in the channel of the sender is most likely a different one and
                // thus unusable since it will not be recognized on the recipient's channel.
                botParty = RoutingDataManager.FindBotPartyByChannelAndConversation(
                    partyToMessage.ChannelId, partyToMessage.ConversationAccount);
            }

            if (botParty != null)
            {
                messageActivity.From = botParty.ChannelAccount;

                MessagingUtils.ConnectorClientAndMessageBundle bundle =
                    MessagingUtils.CreateConnectorClientAndMessageActivity(
                        partyToMessage.ServiceUrl, messageActivity);

                return(await bundle.connectorClient.Conversations.SendToConversationAsync(
                           (Activity)bundle.messageActivity));
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Tries to send the given message to the given party using this bot on the same channel
        /// as the party who the message is sent to.
        /// </summary>
        /// <param name="partyToMessage">The party to send the message to.</param>
        /// <param name="messageText">The message content.</param>
        /// <returns>A valid ResourceResponse instance, if successful. Null in case of an error.</returns>
        public async Task <ResourceResponse> SendMessageToPartyByBotAsync(Party partyToMessage, string messageText)
        {
            Party botParty = null;

            if (partyToMessage != null)
            {
                botParty = RoutingDataManager.FindBotPartyByChannelAndConversation(
                    partyToMessage.ChannelId, partyToMessage.ConversationAccount);
            }

            if (botParty != null)
            {
                MessagingUtils.ConnectorClientAndMessageBundle bundle =
                    MessagingUtils.CreateConnectorClientAndMessageActivity(
                        partyToMessage, messageText, botParty?.ChannelAccount);

                return(await SendAsync(bundle));
            }

            return(null);
        }
示例#3
0
        /// <summary>
        /// Sends a message activity to the conversation using the given bundle.
        /// </summary>
        /// <param name="bundle">The bundle containing the connector client and the message activity to send.</param>
        /// <returns>A valid ResourceResponse instance, if successful. Null in case of an error.</returns>
        protected virtual async Task <ResourceResponse> SendAsync(
            MessagingUtils.ConnectorClientAndMessageBundle bundle)
        {
            ResourceResponse resourceResponse = null;

            try
            {
                resourceResponse =
                    await bundle.connectorClient.Conversations.SendToConversationAsync(
                        (Activity)bundle.messageActivity);
            }
            catch (UnauthorizedAccessException e)
            {
                System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
            }

            return(resourceResponse);
        }