Пример #1
0
        public static async Task <ResourceSet <MessageResource> > GetMessagesAsync(ITwilioRestClient client, string from = null, string to = null, string accountSid = null, DateTime?dateSent = null, DateTime?dateSentAfter = null, DateTime?dateSentBefore = null, long?limit = null)
        {
            var fromNumber = from != null ? new PhoneNumber(from) : null;
            var toNumber   = to != null ? new PhoneNumber(to) : null;
            var options    = new ReadMessageOptions
            {
                PathAccountSid = accountSid,
                DateSent       = dateSent,
                DateSentAfter  = dateSentAfter,
                DateSentBefore = dateSentBefore,
                From           = fromNumber,
                Limit          = limit,
                To             = toNumber,
                PageSize       = null
            };

            return(await MessageResource.ReadAsync(options, client));
        }
Пример #2
0
        public async Task <HealthCheckStatus> CheckHealthAsync(CancellationToken cancellationToken = default)
        {
            _logger.LogInformation("Making Twilio service health check");

            try
            {
                _logger.LogDebug("Getting 1 message from twilio");
                var resource = await MessageResource.ReadAsync(new ReadMessageOptions
                {
                    Limit = 1
                });

                _logger.LogDebug("Successfully read {num} messages", resource.Count());

                _logger.LogInformation("Health check passed");
                return(new HealthCheckStatus(HealthStatus.Healthy));
            }
            catch (ApiException e)
            {
                _logger.LogError(e, "Twilio service health check failed: {code} - {info}", e.Code, e.MoreInfo);
                return(new HealthCheckStatus(HealthStatus.Unhealthy, e.MoreInfo));
            }
        }
Пример #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest httpRequest,
            ExecutionContext executionContext
            )
        {
            _genericConfig = StartupHelper.GetConfig(executionContext);

            string  requestBody   = await new StreamReader(httpRequest.Body).ReadToEndAsync();
            dynamic requestObject = JsonConvert.DeserializeObject(requestBody);

            // connect to Twilio
            TwilioClient.Init(_genericConfig.TwilioAccountSid, _genericConfig.TwilioAuthToken);

            // query twilio api ... and get distinct phone numbers of whatsapp users subscribed to chat
            ResourceSet <MessageResource> messageCollection = await MessageResource.ReadAsync(to : _genericConfig.TwilioWhatsAppPhoneNumber);

            IEnumerable <PhoneNumber> distinctPhoneNumbers = messageCollection.GroupBy(a => a.From.ToString()).Select(b => b.FirstOrDefault().From);

            foreach (var phoneNumber in distinctPhoneNumbers)
            {
                if (phoneNumber.ToString() == requestObject.User.ToString())
                {
                    // if message originated from whatsapp, don't send a copy straight back to sender
                    continue;
                }

                // send message to whatsapp user
                var message = MessageResource.Create(
                    from: new Twilio.Types.PhoneNumber(_genericConfig.TwilioWhatsAppPhoneNumber),
                    body: $"{requestObject.User} : {requestObject.Message}",
                    to: phoneNumber
                    );
            }

            return((ActionResult) new OkResult());
        }