Example #1
0
        public static void Run(
            [QueueTrigger("emails", Connection = "AzureWebJobsStorage")] EmailDetails myQueueItem,
            TraceWriter log,
            [SendGrid(ApiKey = "SendGridApiKey")]
            out Mail mail)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");

            mail = new Mail();
            var personalization = new Personalization();

            personalization.AddTo(new Email(myQueueItem.Email));
            mail.AddPersonalization(personalization);

            var sb = new StringBuilder();

            sb.Append($"Hi {myQueueItem.Name},");
            sb.Append($"<p>You're invited to join us on {myQueueItem.EventDateAndTime} at {myQueueItem.Location}.</p>");
            sb.Append($"<p>Let us know if you can make it by clicking <a href=\"{myQueueItem.ResponseUrl}\">here</a></p>");
            log.Info(sb.ToString());

            var messageContent = new Content("text/html", sb.ToString());

            mail.AddContent(messageContent);
            mail.Subject = "Your invitation...";
            mail.From    = new Email("*****@*****.**");
        }
Example #2
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequestMessage req,
            [Queue("emails", Connection = "AzureWebJobsStorage")]
            IAsyncCollector <EmailDetails> emailsQueue,
            [Table("eventsTable", Connection = "AzureWebJobsStorage")]
            IAsyncCollector <EventTableEntity> eventsTable,
            TraceWriter log)
        {
            log.Info("CreateEvent function  is processing a request.");

            var eventDetails = await req.Content.ReadAsAsync <EventDetails>();

            var responses = new List <Response>();
            var eventId   = Guid.NewGuid().ToString("n");

            foreach (var invitee in eventDetails.Invitees)
            {
                log.Info($"Inviting {invitee.Name} ({invitee.Email})");
                var accessCode   = Guid.NewGuid().ToString("n");
                var emailDetails = new EmailDetails
                {
                    EventDateAndTime = eventDetails.EventDateAndTime,
                    Location         = eventDetails.Location,
                    Name             = invitee.Name,
                    Email            = invitee.Email,
                    ResponseUrl      = $"https://sk-workshop.azurewebsites.net/index.html?eventId={eventId}&code={accessCode}"
                };
                await emailsQueue.AddAsync(emailDetails);

                responses.Add(new Response()
                {
                    Name         = invitee.Name,
                    Email        = invitee.Email,
                    IsPlaying    = "unknown",
                    ResponseCode = accessCode
                });
            }

            await eventsTable.AddAsync(new EventTableEntity()
            {
                PartitionKey     = "event",
                RowKey           = eventId,
                EventDateAndTime = eventDetails.EventDateAndTime,
                Location         = eventDetails.Location,
                ResponseJson     = JsonConvert.SerializeObject(responses)
            });

            log.Info("CreateEvent function processed a request.");

            return(req.CreateResponse(HttpStatusCode.OK));
        }