title | description | services | documentationcenter | author | manager | keywords | ms.service | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Azure Functions Twilio binding |
Understand how to use Twilio bindings with Azure Functions. |
functions |
na |
craigshoemaker |
jeconnoc |
azure functions, functions, event processing, dynamic compute, serverless architecture |
azure-functions |
multiple |
reference |
07/09/2018 |
cshoe |
H1Hack27Feb2017 |
This article explains how to send text messages by using Twilio bindings in Azure Functions. Azure Functions supports output bindings for Twilio.
[!INCLUDE intro]
The Twilio bindings are provided in the Microsoft.Azure.WebJobs.Extensions.Twilio NuGet package, version 1.x. Source code for the package is in the azure-webjobs-sdk GitHub repository.
[!INCLUDE functions-package]
The Twilio bindings are provided in the Microsoft.Azure.WebJobs.Extensions.Twilio NuGet package, version 3.x. Source code for the package is in the azure-webjobs-sdk GitHub repository.
[!INCLUDE functions-package-v2]
See the language-specific example:
The following example shows a C# function that sends a text message when triggered by a queue message.
[FunctionName("QueueTwilio")]
[return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX" )]
public static SMSMessage Run(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] JObject order,
TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {order}");
var message = new SMSMessage()
{
Body = $"Hello {order["name"]}, thanks for your order!",
To = order["mobileNumber"].ToString()
};
return message;
}
This example uses the TwilioSms
attribute with the method return value. An alternative is to use the attribute with an out SMSMessage
parameter or an ICollector<SMSMessage>
or IAsyncCollector<SMSMessage>
parameter.
The following example shows a Twilio output binding in a function.json file and a C# script function that uses the binding. The function uses an out
parameter to send a text message.
Here's binding data in the function.json file:
Example function.json:
{
"type": "twilioSms",
"name": "message",
"accountSid": "TwilioAccountSid",
"authToken": "TwilioAuthToken",
"to": "+1704XXXXXXX",
"from": "+1425XXXXXXX",
"direction": "out",
"body": "Azure Functions Testing"
}
Here's C# script code:
#r "Newtonsoft.Json"
#r "Twilio.Api"
using System;
using Newtonsoft.Json;
using Twilio;
public static void Run(string myQueueItem, out SMSMessage message, TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {myQueueItem}");
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
dynamic order = JsonConvert.DeserializeObject(myQueueItem);
string msg = "Hello " + order.name + ", thank you for your order.";
// Even if you want to use a hard coded message and number in the binding, you must at least
// initialize the SMSMessage variable.
message = new SMSMessage();
// A dynamic message can be set instead of the body in the output binding. In this example, we use
// the order information to personalize a text message to the mobile number provided for
// order status updates.
message.Body = msg;
message.To = order.mobileNumber;
}
You can't use out parameters in synchronous code. Here's an asynchronous C# script code example:
#r "Newtonsoft.Json"
#r "Twilio.Api"
using System;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Twilio;
public static async Task Run(string myQueueItem, IAsyncCollector<SMSMessage> message, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
dynamic order = JsonConvert.DeserializeObject(myQueueItem);
string msg = "Hello " + order.name + ", thank you for your order.";
// Even if you want to use a hard coded message and number in the binding, you must at least
// initialize the SMSMessage variable.
SMSMessage smsText = new SMSMessage();
// A dynamic message can be set instead of the body in the output binding. In this example, we use
// the order information to personalize a text message to the mobile number provided for
// order status updates.
smsText.Body = msg;
smsText.To = order.mobileNumber;
await message.AddAsync(smsText);
}
The following example shows a Twilio output binding in a function.json file and a JavaScript function that uses the binding.
Here's binding data in the function.json file:
Example function.json:
{
"type": "twilioSms",
"name": "message",
"accountSid": "TwilioAccountSid",
"authToken": "TwilioAuthToken",
"to": "+1704XXXXXXX",
"from": "+1425XXXXXXX",
"direction": "out",
"body": "Azure Functions Testing"
}
Here's the JavaScript code:
module.exports = function (context, myQueueItem) {
context.log('Node.js queue trigger function processed work item', myQueueItem);
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
var msg = "Hello " + myQueueItem.name + ", thank you for your order.";
// Even if you want to use a hard coded message and number in the binding, you must at least
// initialize the message binding.
context.bindings.message = {};
// A dynamic message can be set instead of the body in the output binding. In this example, we use
// the order information to personalize a text message to the mobile number provided for
// order status updates.
context.bindings.message = {
body : msg,
to : myQueueItem.mobileNumber
};
context.done();
};
See the language-specific example:
The following example shows a C# function that sends a text message when triggered by a queue message.
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
namespace TwilioQueueOutput
{
public static class QueueTwilio
{
[FunctionName("QueueTwilio")]
[return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
public static CreateMessageOptions Run(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] JObject order,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {order}");
var message = new CreateMessageOptions(new PhoneNumber(order["mobileNumber"].ToString()))
{
Body = $"Hello {order["name"]}, thanks for your order!"
};
return message;
}
}
}
This example uses the TwilioSms
attribute with the method return value. An alternative is to use the attribute with an out CreateMessageOptions
parameter or an ICollector<CreateMessageOptions>
or IAsyncCollector<CreateMessageOptions>
parameter.
The following example shows a Twilio output binding in a function.json file and a C# script function that uses the binding. The function uses an out
parameter to send a text message.
Here's binding data in the function.json file:
Example function.json:
{
"type": "twilioSms",
"name": "message",
"accountSidSetting": "TwilioAccountSid",
"authTokenSetting": "TwilioAuthToken",
"from": "+1425XXXXXXX",
"direction": "out",
"body": "Azure Functions Testing"
}
Here's C# script code:
#r "Newtonsoft.Json"
#r "Twilio"
#r "Microsoft.Azure.WebJobs.Extensions.Twilio"
using System;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
public static void Run(string myQueueItem, out CreateMessageOptions message, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
dynamic order = JsonConvert.DeserializeObject(myQueueItem);
string msg = "Hello " + order.name + ", thank you for your order.";
// You must initialize the CreateMessageOptions variable with the "To" phone number.
message = new CreateMessageOptions(new PhoneNumber("+1704XXXXXXX"));
// A dynamic message can be set instead of the body in the output binding. In this example, we use
// the order information to personalize a text message.
message.Body = msg;
}
You can't use out parameters in synchronous code. Here's an asynchronous C# script code example:
#r "Newtonsoft.Json"
#r "Twilio"
#r "Microsoft.Azure.WebJobs.Extensions.Twilio"
using System;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
public static async Task Run(string myQueueItem, IAsyncCollector<CreateMessageOptions> message, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
dynamic order = JsonConvert.DeserializeObject(myQueueItem);
string msg = "Hello " + order.name + ", thank you for your order.";
// You must initialize the CreateMessageOptions variable with the "To" phone number.
CreateMessageOptions smsText = new CreateMessageOptions(new PhoneNumber("+1704XXXXXXX"));
// A dynamic message can be set instead of the body in the output binding. In this example, we use
// the order information to personalize a text message.
smsText.Body = msg;
await message.AddAsync(smsText);
}
The following example shows a Twilio output binding in a function.json file and a JavaScript function that uses the binding.
Here's binding data in the function.json file:
Example function.json:
{
"type": "twilioSms",
"name": "message",
"accountSidSetting": "TwilioAccountSid",
"authTokenSetting": "TwilioAuthToken",
"from": "+1425XXXXXXX",
"direction": "out",
"body": "Azure Functions Testing"
}
Here's the JavaScript code:
module.exports = function (context, myQueueItem) {
context.log('Node.js queue trigger function processed work item', myQueueItem);
// In this example the queue item is a JSON string representing an order that contains the name of a
// customer and a mobile number to send text updates to.
var msg = "Hello " + myQueueItem.name + ", thank you for your order.";
// Even if you want to use a hard coded message in the binding, you must at least
// initialize the message binding.
context.bindings.message = {};
// A dynamic message can be set instead of the body in the output binding. The "To" number
// must be specified in code.
context.bindings.message = {
body : msg,
to : myQueueItem.mobileNumber
};
context.done();
};
In C# class libraries, use the TwilioSms attribute.
For information about attribute properties that you can configure, see Configuration. Here's a TwilioSms
attribute example in a method signature:
[FunctionName("QueueTwilio")]
[return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
public static CreateMessageOptions Run(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] JObject order, ILogger log)
{
...
}
For a complete example, see C# example.
The following table explains the binding configuration properties that you set in the function.json file and the TwilioSms
attribute.
v1 function.json property | v2 function.json property | Attribute property | Description |
---|---|---|---|
type | type | must be set to twilioSms . |
|
direction | direction | must be set to out . |
|
name | name | Variable name used in function code for the Twilio SMS text message. | |
accountSid | accountSidSetting | AccountSidSetting | This value must be set to the name of an app setting that holds your Twilio Account Sid e.g. TwilioAccountSid. If not set, the default app setting name is "AzureWebJobsTwilioAccountSid". |
authToken | authTokenSetting | AuthTokenSetting | This value must be set to the name of an app setting that holds your Twilio authentication token e.g. TwilioAccountAuthToken. If not set, the default app setting name is "AzureWebJobsTwilioAuthToken". |
to | N/A - specify in code | To | This value is set to the phone number that the SMS text is sent to. |
from | from | From | This value is set to the phone number that the SMS text is sent from. |
body | body | Body | This value can be used to hard code the SMS text message if you don't need to set it dynamically in the code for your function. |
[!INCLUDE app settings to local.settings.json]
[!div class="nextstepaction"] Learn more about Azure functions triggers and bindings