public async Task <string> Execute(string phoneNumber) { if (string.IsNullOrWhiteSpace(phoneNumber)) { throw new ArgumentNullException(nameof(phoneNumber)); } var confirmationCode = new ConfirmationCode { Value = await GetCode(), IssueAt = DateTime.UtcNow, ExpiresIn = 300, Subject = phoneNumber }; var message = string.Format(_smsAuthenticationOptions.Message, confirmationCode.Value); try { await _twilioClient.SendMessage(_smsAuthenticationOptions.TwilioSmsCredentials, phoneNumber, message); } catch (Exception ex) { _eventSource.Failure(ex); throw new IdentityServerException(ErrorCodes.UnhandledExceptionCode, "the twilio account is not properly configured"); } if (!await _confirmationCodeStore.Add(confirmationCode)) { throw new IdentityServerException(ErrorCodes.UnhandledExceptionCode, ErrorDescriptions.TheConfirmationCodeCannotBeSaved); } _eventSource.GetConfirmationCode(confirmationCode.Value); return(confirmationCode.Value); }
/// <summary> /// Sends activities to the conversation. /// </summary> /// <param name="turnContext">The context object for the turn.</param> /// <param name="activities">The activities to send.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects /// or threads to receive notice of cancellation.</param> /// <returns>A task that represents the work queued to execute.</returns> /// <remarks>If the activities are successfully sent, the task result contains /// an array of <see cref="ResourceResponse"/> objects containing the SIDs that /// Twilio assigned to the activities.</remarks> /// <seealso cref="ITurnContext.OnSendActivities(SendActivitiesHandler)"/> public override async Task <ResourceResponse[]> SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken) { var responses = new List <ResourceResponse>(); foreach (var activity in activities) { if (activity.Type == ActivityTypes.Message) { var messageOptions = TwilioHelper.ActivityToTwilio(activity, _options.TwilioNumber); var res = await _twilioApi.SendMessage(messageOptions).ConfigureAwait(false); var response = new ResourceResponse() { Id = res, }; responses.Add(response); } else { throw new ArgumentException("Unknown message type of Activity.", nameof(activities)); } } return(responses.ToArray()); }