Function app - not sending SMS with Twilio

wavemaster 6 Reputation points
2020-09-29T15:50:41.683+00:00

I am using the demo code sample here

My code has no exceptions, but it appears to not send any messages judging by the Twilio logs

Is this code complete or do I need to do anything else to make it send a SMS?

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([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, out CreateMessageOptions message, ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myTimer}");

// 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.  

string msg = "Hello thank you for your order.";  

// You must initialize the CreateMessageOptions variable with the "To" phone number.  
message = new CreateMessageOptions(new PhoneNumber("+1xxxxx"));  

// 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;  

}

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,605 questions
{count} vote

3 answers

Sort by: Most helpful
  1. JayaC-MSFT 5,526 Reputation points
    2020-10-13T12:37:00.17+00:00

    @wavemaster As discussed in our offline session we can follow the official document functions-bindings-twilio In this document we have a queue trigger example. As per requirement I have tried with a timer trigger and this works fine.

    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(TimerInfo myTimer,out CreateMessageOptions message, ILogger log)  
    {  
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");  
      
        string msg = "Hello there! This is a test message";  
      
        // You must initialize the CreateMessageOptions variable with the "To" phone number.  
        message = new CreateMessageOptions(new PhoneNumber("+9xxxxxxxxxx"));  
      
        // 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;  
    }  
    

    functions.json:

    {  
      "bindings": [  
        {  
          "name": "myTimer",  
          "type": "timerTrigger",  
          "direction": "in",  
          "schedule": "0 */5 * * * *"  
        },  
        {  
          "type": "twilioSms",  
          "name": "message",  
          "accountSidSetting": "accountSidSetting",  
          "authTokenSetting": "authToken",  
          "from": "+1xxxxxxxxx",  
          "direction": "out",  
          "body": "Azure Functions Testing"  
        }  
      ]  
    }  
    

    Note : In this case I am sending a message from Twillio to my number, so I have tested with From - "Twillio number" and To- "My number".

    Also, these settings values should be saved in app settings and use the name of the app settings in the functions.json :

    31929-image.png

    If this helped , please "accept the answer" and "up-vote " , so that it helps others in the community.

    0 comments No comments

  2. Marcus GV 6 Reputation points
    2020-10-29T09:24:44.83+00:00

    This is not an anser, but I only get 1000 characters in the comments and I want to show that I have the same issue.

    Using Node.js Javascript, I also run the function, it logs a successful execution, but I don't even give it a real account SID, Token or From number yet, so it's actually not working at all. This kind of "silent failure" is really bad as it will seriously hamper our ability to detect issues in production.

    function.json:

    {  
      "bindings": [  
        {  
          "connection": "AZURE_SERVICEBUS_CONNECTION_STRING",  
          "queueName": "sms-messages-queue",  
          "name": "queueBinding",  
          "type": "serviceBusTrigger",  
          "direction": "in"  
        },  
        {  
          "name": "twilioBinding",  
          "type": "twilioSms",  
          "accountSidSetting": "TwilioAccountSid",  
          "authTokenSetting": "TwilioAuthToken",  
          "from": "+1425XXXXXXX",  
          "direction": "out"  
        }  
      ],  
      "scriptFile": "../dist/index.js"  
    }  
    

    Function code:

    import { AzureFunction, Context } from "@azure/functions";  
    // import { getExtendedLoggerFromFunction } from "../common/utils/get-extended-logger-from-function";  
    // import { SMSMessage } from "../models/sms-models";  
      
    interface SMSQueueItem {  
      body: string;  
      to: string;  
    }  
      
    const messageProcessor: AzureFunction = async function (  
      context: Context,  
      myQueueItem: SMSQueueItem  
    ): Promise<void> {  
      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.  
      
      // 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 = myQueueItem;  
      
      context.done();  
    };  
      
    export default messageProcessor;  
    

    logs:

    35908-azure-logs.png

    "Succeeded", but not really!

    Is there a way we can make sure we get the results from the twilio binding, log them, and fail or succeed the function based on the outcome of the binding execution? I would expect this to be default behavior.

    0 comments No comments

  3. wavemaster 6 Reputation points
    2020-10-29T13:58:10.87+00:00

    This is what I have learned so far:

    1. you cannot copy code from a tutorial into the azure portal: never got anything to work doing this. It appears this leaves non-visible characters. IMO the function gets dirty and I have never been able to clean things up. It fails silently. When I type everything into the portal I was able to get the tutorial code to work.
    2. There are 3 different versions of the function app, most tutorials you will find were based on V1. V2 and V3 versions work differently, so the older sample code won't work.. I suggest that Microsoft at least starts mentioning the version of function app the sample code was written for.