Skip to content

Instantly share code, notes, and snippets.

View KevinDJones's full-sized avatar

KJ Jones KevinDJones

View GitHub Profile
public class MessageInput
{
public string Phone { get; set; } // Phone number to send to.
public string Message { get; set; } // The message to send
public string SmsKey { get; set; } // Key unique to each message
}
@KevinDJones
KevinDJones / Starters.cs
Last active October 24, 2018 14:48
HTTP Trigger to Send Message
[FunctionName("SendMessageStarter")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]
HttpRequestMessage req,
[OrchestrationClient] DurableOrchestrationClient starter,
ILogger log)
{
var input = await req.Content.ReadAsAsync<MessageInput>();
var orchestrationId = await starter.StartNewAsync("O_SendMessage", input);
return starter.CreateCheckStatusResponse(req, orchestrationId);
[FunctionName("O_SendMessage")]
public static async Task SendMessageOrchestrator(
[OrchestrationTrigger] DurableOrchestrationContextBase context,
ILogger log)
{
MessageInput messageInput = context.GetInput<MessageInput>();
string smsKey = await context.CallActivityAsync<string>("A_SaveTwilioMapping", context.InstanceId);
messageInput.SmsKey = smsKey;
[FunctionName("A_SaveTwilioMapping")]
public static async Task<string> SaveTwilioMapping(
[ActivityTrigger] string orchestrationId,
[Table(MessageMapping.TableName)] IAsyncCollector<MessageMapping> table)
{
var mapping = new MessageMapping
{
PartitionKey = "Sms",
RowKey = Guid.NewGuid().ToString("N"),
OrchestrationId = orchestrationId
public class MessageMapping
{
public const string TableName = "SentMessages";
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string OrchestrationId { get; set; }
}
[FunctionName("A_SendTwilioText")]
public static async Task SendTwilioText(
[ActivityTrigger] MessageInput input,
[TwilioSms] IAsyncCollector<CreateMessageOptions> message,
ILogger log)
{
log.LogWarning($"Twilio Activity. Sending message");
CreateMessageOptions smsText = new CreateMessageOptions(new PhoneNumber(input.Phone))
{
[FunctionName("TwilioHandler")]
public static async Task<HttpResponseMessage> Webhook(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "TwilioHandler/{id}")]
HttpRequestMessage req,
[OrchestrationClient] DurableOrchestrationClient client,
[Table(MessageMapping.TableName, "Sms", "{id}")] MessageMapping mapping,
ILogger log)
{
var content = await req.Content.ReadAsStringAsync();
var formBody = ParseForm(content);
[FunctionName("ColdStarter")]
public static async Task Run(
[TimerTrigger("* */15 * * * *")]TimerInfo myTimer,
ILogger log)
{
var client = new HttpClient();
await client.GetAsync(Environment.GetEnvironmentVariable("FunctionUrl"));
log.LogInformation($"Pinged Function at: {DateTime.Now}");
}
[assembly: WebJobsStartup(typeof(Startup))]
namespace YOUR.NAMESPACE
{
internal class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.AddDependencyInjection(ConfigureServices);
}
[FunctionName("A_SendMail")]
public static async Task<string> SendMail(
[ActivityTrigger] object input,
[Inject]ISendGridClient client,
ILogger log)
{}