1

I have found a few examples of context.getTwilioClient(), but I have not been able to locate any online documentation. It's probably right under my nose, but it is eluding me. In particular, I'm trying to get information about workers in a Task Router queue (i.e., how many workers are in the different statuses and how many workers are available), but having the documentation will help with future projects.

I found some documentation saying that the context.getTwilioClient(), "enables you to invoke the getTwilioClient method on the context object for a fully-initialized Twilio REST API client." (https://support.twilio.com/hc/en-us/articles/115007737928-Getting-Started-with-Twilio-Functions)

It then shows this example, but there is no implementation of "messages" when I attempt to run this code:

var client = context.getTwilioClient();
client.messages.create({
  to: '+12025551212',
  from: '+12065551212',
  body: "hello world!"})

Thanks.

tcbeaton
  • 85
  • 7

1 Answers1

1

The messages property should be on the client. getTwilioClient returns the Twilio helper library for Node.js.

I just created a Function with your code, and it worked as expected, meaning that I got the SMS, however, the function did time out because the callback was never invoked. To end the function invocation and respond to the caller, make sure you always invoke the callback function, like this:

exports.handler = function(context, event, callback) {
  var client = context.getTwilioClient();
  client.messages.create({
    to: '+1xxxxxxxxxx',
    from: '+1xxxxxxxxxxx',
    body: "hello world!"})
  .then((message) => {
    console.log('SMS successfully sent');
    console.log(message.sid);
    // Make sure to only call `callback` once everything is finished, and to pass
    // null as the first parameter to signal successful execution.
    return callback(null, `Success! Message SID: ${message.sid}`);
  })
  .catch((error) => {
    console.error(error);
    return callback(error);
  });
};

You can learn more about the callback function here.

If you still encounter this issue, can you tell use what Node Version you're using and which module dependencies and their versions?

You can find these details at Settings & More > Dependencies in your Twilio Functions Service.

Swimburger
  • 6,681
  • 6
  • 36
  • 63
  • I am 100% baffled and confused. Under "Functions and Assets", I have a service with two functions. When editing the first (from a few days ago), if I type `client.me` the autocomplete displays only `message`. I created a new function and copied your code, updating the phone numbers, and when I type `client.me`, the autocomplete shows both `message` and `messages`. I copied the code from the first function into the second and I only have one autocomplete, `message`. Since they're in the same service, they are both using Node.js v14, although changing to v16 makes no difference. – tcbeaton Dec 18 '22 at 00:01
  • The other modules are the standard ones added when creating the service. lodash 4.17.11, twili 3.29.2, xmldom 0.1.27, @twilio/runtime-handler 1.0.1, and util 0.11.0 . – tcbeaton Dec 18 '22 at 00:03
  • I see, so is it the autocompletion that isn't working, or does the given code not work at all? – Swimburger Dec 18 '22 at 03:38
  • 1
    I believe it is an issue with auto-completion. I created a third function, hand typing the lines and when I got to the line beginning with `client.me`, there were no auto-completion options. I continued typing the function, saved and deployed it, and it operated correctly by sending the SMS message. Going back to the third function, and now typing `client.me` showed both `message` and `messages`. So, it appears that the autocomplete doesn't work until the function is saved and deployed first, and only if the function already contains `client.messages`. – tcbeaton Dec 18 '22 at 23:10
  • Interesting! So maybe the modules are only pulled down when it is deployed, and then the autocomplete starts working because the modules are now present. I'll pass on this feedback. – Swimburger Dec 19 '22 at 03:35
  • Cool, thanks. If I could go back to the original message. Early on, you mentioned the Twilio helper library for Node.js. I ended up in a circular loop, clicking links and returning to the same pages. Can you send a link to the documentation for client.messages (for example), which I have not found. I'm ultimately looking for documentation on the methods and attributes for the client object, not just messages. – tcbeaton Dec 19 '22 at 16:31
  • You can find [the docs for the node library here](https://www.twilio.com/docs/libraries/reference/twilio-node/). Then select the version you're using, let's say [3.84.0](https://www.twilio.com/docs/libraries/reference/twilio-node/3.84.0/), then click on [the `Twilio` class](https://www.twilio.com/docs/libraries/reference/twilio-node/3.84.0/Twilio.html) where you can find your `messages` property. That takes you to [the `MessagesList` class](https://www.twilio.com/docs/libraries/reference/twilio-node/3.84.0/Twilio.Api.V2010.AccountContext.MessageList.html) where you can find the `create` method – Swimburger Dec 19 '22 at 20:44
  • It's not that easy to find, but I hope this helps! – Swimburger Dec 19 '22 at 20:45
  • 1
    That helped; thank you! I missed clicking on the Twilio class, so I never saw anything further into the hierarchy. – tcbeaton Dec 20 '22 at 00:05
  • If this answers your question, kindly accept it! We can't wait to see what you build! – Swimburger Dec 20 '22 at 01:42