Mautic Integration with Twilio to send message to Whatsapp

Your software
My Mautic version is: 2.16.2
My PHP version is: 7.0.31

Your problem
My problem is:

I have mautic installation that work with twilio for send sms. Works fine. But I want to send message through whatsapp. I change in the mautic plugin the sender number and include the “whatsaap:” follow with the number sender approved on twilio. But when I send it show me an error message on Mautic.

[HTTP 400] Unable to create record: The ‘From’ number whatsapp:+15089805226 is not a valid phone number, shortcode, or alphanumeric sender ID.

But when I put only the number it work fine +15089805226. I made the validation process on Twilio and they approved to me send whatsapp message, and give instruction of change this on the send number.
I think that the library or the SDK of the plug in of twilio on mautic its too old? can you help to me?

Thanks

These errors are showing in the log: Yes

Steps I have tried to fix the problem:

1 Like

hi jpgiron

The Twilio integration with Mautic works only for SMS. if you want to send whatsapp messages through mautic to Twilio you need a different approach. You should use a webhook action in a campaign that send the the payload to a Twilio endpoint that is intended to handle messages for WhatsApp

the paramters are as follows:

Twilio endpoint for whatsapp messaging:
https://api.twilio.com/2010-04-01/Accounts/xxxxxxxxxxxxxxx/Messages.json

(xxxxxxxxxxxxxxx is your Twilio client id - same one you use in Mautic)
Method is POST
Headers:
autorization: basic twilioclientid:twilioclientsecret (twilio credentials (along with “:”) should be encoded in base64. you can do it here https://www.base64encode.org)
content-type: application/x-www-form-urlencoded

Data
From: your number (whatsapp+number)
To: number of recepient
Body: content of message

all Data fields support mautic tokens for fields (as you can see from screenshot)

5 Likes

Thanks! This is great.

Another question… mautic can recieve webhooks? i search in the documentation but i dont find anything…

Not possible out of the box. You can create “webhook-like” mechanisms with Mautic’s API

Do you know how? can you make a pay job for this?

Hi @jpgiron,

I find your approach helpful so I did a bit of digging in the Twilio integration code so here’s what I did (this might be a bit of a hack and not upgrade safe but hopefully helps):

Find this file: /app/bundles/SmsBundle/Integration/Twilio/TwilioTransport.php
Then change the sendSms() method with the following:

public function sendSms(Lead $lead, $content)
{
    $number = $lead->getLeadPhoneNumber();

    if ($number === null) {
        return false;
    }

    try {
        $this->configureClient();
        
        /***** Add Support to WhatsApp *****/
        $prefix = '';
        if($number){
            if(strlen($number)>9){
                if(substr($number, 0, 9) === 'whatsapp:'){
                    $prefix = 'whatsapp:';
                    $number = substr($number, 9);
                }
            }
        }
        /***********************************/
        
        $this->client->messages->create(
            $prefix.$this->sanitizeNumber($number),
            [
                'from' => $prefix.$this->sendingPhoneNumber,
                'body' => $content,
            ]
        );

        return true;
    } catch (NumberParseException $exception) {
        $this->logger->addWarning(
            $exception->getMessage(),
            ['exception' => $exception]
        );

        return $exception->getMessage();
    } catch (ConfigurationException $exception) {
        $message = ($exception->getMessage()) ? $exception->getMessage() : 'mautic.sms.transport.twilio.not_configured';
        $this->logger->addWarning(
            $message,
            ['exception' => $exception]
        );

        return $message;
    } catch (TwilioException $exception) {
        $this->logger->addWarning(
            $exception->getMessage(),
            ['exception' => $exception]
        );

        return $exception->getMessage();
    }
}

Hello, i made this modification, but give me the same error.

Último error de ejecución: [HTTP 400] Unable to create record: The ‘From’ number whatsapp:+15089805226 is not a valid phone number, shortcode, or alphanumeric sender ID.

Hi @jpgiron, you should set your ‘From’ number as +15089805226 and not whatsapp:+15089805226. If you applied the code block above, it should automatically prepend the ‘whatsapp:’ prefix to your ‘From’ number.

In our case, we have the same twilio number for sending SMS and sending via WhatsApp. If the Contact’s number has a ‘whatsapp:’ prefixed to it, then the code above will prepend ‘whatsapp:’ to the ‘From’ number and sends via WhatsApp, else, it will use the ‘From’ number as is then sends the message as SMS.

Hello, i tried with whatsapp and get the error and without it didn’t work .

I checked why and found that for some reason my mautic installation did not capture the sms answers of text message when the user reply with something. Searching on google, I found that this issue

and check each change on each files. The majority of the changes its on mautic, but some files doesn’t have this changes. As this:

app/bundles/SmsBundle/Api/TwilioApi.php
app/bundles/SmsBundle/Test/Sms/TransportChainTest.php

does not have, this line, that call to the file you indicate i need change.

use Mautic\SmsBundle\Integration\Twilio\TwilioTransport;

with this, the change you indicate, now work. Also work when mautic get the answer of sms and/or whatsapp, and execute the instructions on a campaign! Now i can get also replied on whatsapp that execute campaign conditionals.

Very Thanks for your help! I think this work great now!

1 Like

We’re just starting with Twilio integration in Mautic. This sure will help with our campaigns logic. Thanks.

Yes, now works as a basic bot on whatsapp. Capture the answers and execute the campaign for send text message for example, basis on the conditional you put. Now i want to capture some answer and put as a contact field, because all answer get in the history interaction of mautic, but i want to capture some of them to store with the contact and send to another application. Do you have some idea of how to do that?

Hey @OmerGafny

Great post. I know its been a while since you post and Mautic has had a number of releases since then, I went ahead and followed your directions.

However I get a 401 error in the users log card:

I have checked and there are no errors in the mautic.error log or the mautic UI system log.

I went and built the same string using curl in terminal to check if I am able to reach the endpoint and am able to send the message of via WhatsApp.

Any ideas or other ways I can try debug.

I got it working. I had left out two important things:

  1. I did not go ahead and encode by authorization keys using the link you provided.
  2. I forgot to add the word “Basic” to the beginning of the Authorization.

@OmerGafny great hack!! Thank you for sharing.

1 Like

Dit it work for you? Because I’m having the same error code (401), but I have no idea what I did wrong.

What is your mautic version?

Hello, I have this working, now I get the answer from Twilio and obviously I can’t send the message because is a free message form and not a WhatsApp template.

The question is: How do I use the variables?

I mean, for example, my message template is:

Welcome to the team ring {{1}}, in this link www.xxxxx.xxx you will be able to see login to your account and access to the company services and the education platform. We love to see you in here.

So, let’s say WhatsApp already approved that Message template
How do I configure it in the “Body” field and how do I insert the user name (contactfield=firstname) to replace the {{1}} variable in the WhatsApp template.

Thanks!!!

Hi, i just tried to send sms and whatsapp via mautic and i got these errors:

this is campaign

and this is webhook config

what’s wrong? thanks

Hello, i solved my issue and i wrote a PHP code to send whatsapp messages via mautic without third party services.

i posted a tutorial here: Tutorial: send whatsapp template messages using mautic webhook without third party services

1 Like

Did you check that whatsapp only allow send template messages that need to be register before?

You can not send any message. First you need to put as a template, get approval of whatsapp, and after that you can send it.

it seems that you do not put credential.