Beispiel #1
0
        /// <summary>
        /// Creates Twilio SMS message options object from a Bot Framework <see cref="Activity"/>.
        /// </summary>
        /// <param name="activity">The activity.</param>
        /// <param name="twilioNumber">Optional. The Twilio phone number assigned to the bot. If not provided, defaults to Activity.From.Id to allow WhatsApp and other future integrations.</param>
        /// <returns>The Twilio message options object.</returns>
        /// <seealso cref="TwilioAdapter.SendActivitiesAsync(ITurnContext, Activity[], System.Threading.CancellationToken)"/>
        public static TwilioMessageOptions ActivityToTwilio(Activity activity, string twilioNumber = null)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }

            if (string.IsNullOrWhiteSpace(twilioNumber) && string.IsNullOrWhiteSpace(activity.From?.Id))
            {
                throw new ArgumentException($"Either {nameof(twilioNumber)} or {nameof(activity.From.Id)} must be provided.");
            }

            var mediaUrls = new List <Uri>();

            if (activity.Attachments != null)
            {
                mediaUrls.AddRange(activity.Attachments.Select(attachment => new Uri(attachment.ContentUrl)));
            }

            var messageOptions = new TwilioMessageOptions()
            {
                To             = activity.Conversation.Id,
                ApplicationSid = activity.Conversation.Id,
                From           = twilioNumber ?? activity.From.Id,
                Body           = activity.Text
            };

            messageOptions.MediaUrl.AddRange(mediaUrls);

            return(messageOptions);
        }
        /// <summary>
        /// Sends a Twilio SMS message.
        /// </summary>
        /// <param name="messageOptions">An object containing the parameters for the message to send.</param>
        /// <param name="cancellationToken">A cancellation token for the task.</param>
        /// <returns>The SID of the Twilio message sent.</returns>
        public virtual async Task <string> SendMessageAsync(TwilioMessageOptions messageOptions, CancellationToken cancellationToken)
        {
            var createMessageOptions = new CreateMessageOptions(messageOptions.To)
            {
                ApplicationSid = messageOptions.ApplicationSid,
                MediaUrl       = messageOptions.MediaUrl,
                Body           = messageOptions.Body,
                From           = messageOptions.From
            };

            var messageResource = await MessageResource.CreateAsync(createMessageOptions).ConfigureAwait(false);

            return(messageResource.Sid);
        }