Issues using REST Component with Twilio

Hi, I’m trying to use the rest_command to use Twilio service to fire a call for notification purpose.

Trying with curl or with a rest client for Chrome is working but putting the same values in Home Assistant result in an error 400 BAD REQUEST.

in my configuration.yaml I have:

rest_command:
  call_me:
    url: 'https://api.twilio.com/2010-04-01/Accounts/[ACCOUNTSID]/Calls.json'
    method: post
    username: [ACCOUNTSID]
    password: [AUTHTOKEN]
    payload: From=+391234567890&To=+391234567890&Url=http://twimlets.com/message?Message=Hi

In home-assistant.log I find:

17-01-26 18:36:28 homeassistant.components.rest_command: Error 400 on call https://api.twilio.com/2010-04-01/Accounts/[ACCOUNTSID]/Calls.json.

I modified the rest_command.py source file adding some extra logging to better understand and got:

17-01-26 18:36:27 homeassistant.components.rest_command: Payload b'From=+39123456789&To=+39123456789&Url=http://twimlets.com/message?Message=Hi'.
17-01-26 18:36:28 homeassistant.components.rest_command: Error response <ClientResponse(https://api.twilio.com/2010-04-01/Accounts/[ACCOUNTSID]/Calls.json) [400 BAD REQUEST]>
<CIMultiDictProxy('Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Headers': 'Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since', 'Access-Control-Allow-Methods': 'GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Origin': '*', 'Access-Control-Expose-Headers': 'ETag', 'Content-Type': 'application/json', 'Date': 'Thu, 26 Jan 2017 18:36:27 GMT', 'Twilio-Request-Duration': '0.079', 'Twilio-Request-Id': 'RQ7c2e060e3b584426aed99f603afaa8ad', 'X-Powered-By': 'AT-5000', 'X-Shenanigans': 'none', 'Content-Length': '129', 'Connection': 'keep-alive')>

with
_LOGGER.info("Payload %s.", payload)
and
_LOGGER.warning("Error response %s.", request.json())

I’m pretty sure there is something I’m doing wrong but the payload seems good to me.
Twilio API documentation says that the Content-Type header must be set to “application/x-www-form-urlencoded”, don’t know how to set this header in HA anyhow.

Any help is more then welcome :slight_smile:

Hi, author of the notify.twilio_sms platform here,

It would be pretty easy to take my SMS platform and extend it to support calls as well. The only reason I didn’t do that in the first place was because of the requirement to serve TwiML but using Twimlets could be an easy stopgap solution.

If you had any interest in getting involved in an aspect of Home Assistant development i’d recommend this as a super easy project to wet your feet with. The platform is using the well documented official Twilio API library already so you should just need to change occurrences of SMS with call or whatever.

As for your 400, I believe that this is failing because it’s posting JSON instead of as a form like you noticed. I don’t know if there is any easy way to correct the problem with the rest_command platform.

Hi @robbiet480 thank you for your prompt response and trust in my programmer skills.

I’m not writing code since 5/6 years but I’ll give a try starting from your work. I’ll drop you a msg if I need help :yum:

In the meanwhile if someone else has some hints on the rest_command he/she is always welcome

1 Like

Hi @robbiet480 I just made my first call with HA through Twilio modifying your code: it was super easy!

Thank you

I’m going to read the instruction to pull it in

1 Like

Told you! :smiley:

I would say that you should allow users to pass in the URL via the data dictionary. Just do something similar to this line except it should look like this:

url = kwargs.get(ATTR_URL)

You’d need to define ATTR_URL = "url" right under CONF_FROM_NUMBER at the top of the file too.

If the user didn’t set a url in the data dictionary then I would say the call should fail and you should log an error.

As for submitting your work, we have two helpful guides on the website right here and more importantly here.

I’m reading the documentation and setting up the dev environment (vi seems to be not enough :grin:)

I’ve done as you suggest. This is an example of my automations.yaml:

    - service: notify.twilio_call
      data:
        message: 'Something to read'
        target: +1234567890

Like yours :yum: accept accept multiple targets and with message is created the Twimlet url

You should think to create an unique component with SMS and Voice :wink:

1 Like

Looks good! Submit it!

Submitted!

PR https://github.com/home-assistant/home-assistant/pull/5580

It’s my first ever commit to github… hope I did everything correctly.

Thank you!!!