Пример #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">The Twilio phone number assigned to the bot.</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)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }

            if (string.IsNullOrWhiteSpace(twilioNumber))
            {
                throw new ArgumentNullException(nameof(twilioNumber));
            }

            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,
                Body           = activity.Text
            };

            messageOptions.MediaUrl.AddRange(mediaUrls);

            return(messageOptions);
        }
Пример #2
0
        /// <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);
        }