3

Currently trying to create a Whatsapp message to be sent via This custom Javascript code on Zapier shown via Twilio API. It successfully reaches out to twilio but returns "Authentication Error - No credentials provided".

var messagesUrl = "https://api.twilio.com/2010-04-01/Accounts/<accountSID>/Messages.json";
var payload = {
 "To": "whatsapp:<tonumber>",
 "Body" : "Hey This is a test",
  "From" : "whatsapp:<fromnumber>"
};
 var options = {
 "method" : "post",
 "payload" : payload
};

options.headers = {
 "Authorization" : "Basic <accountsid>:<accounttoken>"
};
fetch(messagesUrl, {options, body: JSON.stringify(payload)})
 .then(function(binaryResponse) {
  return binaryResponse.json();
})
.then(function(jsonResponse){
  callback(null, {result: jsonResponse});
}).catch(callback);

What is the best approach to take to simply send a confirmation text on whatsapp to prospects on zapier? Thanks in advance

Conroy
  • 81
  • 1
  • 7

4 Answers4

3

fetch takes an options object as its second argument. You're currently doing:

fetch (messagesUrl, {
  options: {
    method: 'post',
    payload: payload,
    headers: {
      Authorization: '...'
    }
  },
  body: JSON.stringify(payload)
}) // ...

You shouldn't have a nested option object. Assuming the twilio stuff is set up correctly (I have no idea), this should work:

fetch (messagesUrl, {
  method: 'post',
  payload: payload, // probably remove this, it's not part of the syntax (see below)
  headers: {
    Authorization: '...'
  }
  body: JSON.stringify(payload)
}) // ...

all options you can pass to the second argument: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax

xavdid
  • 5,092
  • 3
  • 20
  • 32
2

Thank you for your help, i ended up solving this by using a much simpler Custom web hook POST method, i was over-complicating it. Details on how i did this for anybody else wanting to send whats-app messages through Twilio on Zapier Below:

Method

Post

URL

https://api.twilio.com/2010-04-01/Accounts/<accountSID>/Messages.json

Data

To=whatsapp:%2B<toNumber>&From=whatsapp:%2B<fromNumber>&Body=Put your message body here

Note: The + of your E.164 number will concatenate hence the %2B to replace it

Basic Auth

<AccountSID>|<AccountToken>

Headers

Content-Type | application/x-www-form-urlencoded
Conroy
  • 81
  • 1
  • 7
1

I found this post very very interesting. I also use ZAPIER and TWILIO and I would like to have a customer who registers on my googlesheet receive a confirmation whatsapp message.

I can't configure ZAPIER. I did a lot of tests but I don't understand much about programming and I certainly made a mistake.

When I launch the test from ZAPIER it gives me this error: The request could not be sent to Webhooks by Zapier. The app returned "A 'To' phone number is required.".

I have attached the image with the inserted configuration. Where did I go wrong? How should I correct it?

enter image description here

UPDATE: my approved template on whatsapp is this:

Ciao {{1}}, la tua registrazione è avvenuta con successo.
Ricordati che lo SCONTO che hai scelto è attivo fino al {{2}}. 
Ti aspettiamo - nei prossimi giorni - nel tuo Centro LIGHT di fiducia.

Nell'attesa ti facciamo ancora i migliori Auguri
Staff LIGHT

The generated text is this

Ciao%20%7B%7B1%7D%7D%2C%20la%20tua%20registrazione%20%C3%A8%20avvenuta%20con%20successo.%0ARicordati%20che%20lo%20SCONTO%20che%20hai%20scelto%20%C3%A8%20attivo%20fino%20al%20%7B%7B2%7D%7D.%20%0ATi%20aspettiamo%20-%20nei%20prossimi%20giorni%20-%20nel%20tuo%20Centro%20LIGHT%20di%20fiducia.%0A%0ANell%27attesa%20ti%20facciamo%20ancora%20i%20migliori%20Auguri%F0%9F%8E%81%F0%9F%8E%89%F0%9F%8E%8A%0AStaff%20LIGHT

Error Reported:

ERROR 63016 Failed to send freeform message because you are outside the allowed window. Please use a Template.

DESCRIPTION Failed to send freeform message because you are outside the allowed window. Please use a Template.

  • In your shown example, you are including the %2B and the +, get rid of the plus and the space between it for example. You are currently doing %2B+441234567 Instead do %2B441234567 – Conroy Mar 05 '21 at 16:05
  • @Conroy Thank you so much for your help. I did some tests, I updated the photos above. I seem to have done everything right but it always gives me the same mistake: The request could not be sent to Webhooks by Zapier. The app returned "A 'To' phone number is required.". :-( – Mauro Mancini Mar 05 '21 at 16:15
  • Ah Mauro, its always the small things. You are currently using a POST webhook option by zapier. In order to get this to work, you will need to use a **Custom request**. You can select the **Custom Request** in the webhooks trigger and then follow my steps from above. – Conroy Mar 05 '21 at 16:27
  • @Conroy Super thanks now it's ok. On ZAPIER he gives me TEST OK. I did some tests but I don't get any kind of whatsapp message :-( A doubt. What should I put on "Put your message body here"? A text of your choice or a specific template approved on twilio? In fact, if I go to the Programmable Messaging Dashboard I get this error: (Error: 63016) Failed to send freeform message because you are outside the allowed window. Please use a Template. Oh my God I don't understand much, how should I do? – Mauro Mancini Mar 05 '21 at 17:35
  • You must get templates approved on Twilio by whatsapp first. This video by Yasin Hassanien is really helpful to show you the best ways to work with templates in plaintext once they are approved - https://www.youtube.com/watch?v=fcVmafytF9Y – Conroy Mar 05 '21 at 17:41
  • @Conroy I can't get the message across even with approved Templates :-( I have tried so many times. This is the approved message: "Hello {{1}}, your registration was successful. Remember that the DISCOUNT you have chosen is active until {{2}}. We are waiting for you - in the next days - in your LIGHT Center of trust. In the meantime we wish you our best wishes again Staff LIGHT " Variables 1 and 2 are the ones I take with Zapier from the googlesheet. Am I wrong something? – Mauro Mancini Mar 08 '21 at 17:05
  • @Conroy Perhaps the problem is with the formatting of the text. What should I do if I have a line that breaks? Or a line that remains empty? – Mauro Mancini Mar 08 '21 at 17:43
  • @Conroy For completeness, I uploaded the twilio approved message above and the generated code. Thanks so much for your help. – Mauro Mancini Mar 08 '21 at 18:02
  • When describing errors in technical work, it is better to be specific. **example** approved templates sure, but what error are you receiving? • What error do you get in the zapier task logs? • then check your twilio log to see if both errors are the same? • When you are testing, are you passing in sample variables? it will not pass through successfully if they are null, it can be a whole multitude of things, so sometimes it will be better to test multiple things. – Conroy Mar 08 '21 at 22:17
  • @Conroy sorry. It is always giving me this error: 63016 Failed to send freeform message because you are outside the allowed window. Please use a Template. DESCRIPTION Failed to send freeform message because you are outside the allowed window. Please use a Template. – Mauro Mancini Mar 09 '21 at 06:27
  • Are you trying to use emojis? they would not be supported in plaintext as they would not render in this use case, you will have to get another template approved without any emojis. – Conroy Mar 09 '21 at 16:24
  • @Conroy Thanks so much for your invaluable help. Now I approve a new template message without emoticons. Do you know if you can format the text with bold and / or italics? – Mauro Mancini Mar 09 '21 at 17:03
  • Not that I am aware of. Plain text, in this case, has to remain plain. Until Twilio officially supports WhatsApp on their zapier trigger, you will be unable to do much more than what I've done above.. – Conroy Mar 09 '21 at 19:56
  • @ Conroy Fantastic. Now it works perfectly. Thanks so much. PS: Now I have to figure out how to send images too. :-) – Mauro Mancini Mar 10 '21 at 13:58
  • Great to hear. Happy Automating! – Conroy Mar 10 '21 at 14:51
0

Its better to watch this video to be able to understand the complications around the message templates and zapier customization

You will face some issues if you didn't control the new lines in the templates and to avoid message failure

Yasin Hassanien
  • 4,055
  • 1
  • 21
  • 17