Mautic Integration with Twilio to send message to Whatsapp

Hello to all

My Mautic Version: 2.15.2
My PHP Version: 7.1.32

I made my mautic installation work with Twilio to send Whatsapp Template Message

I go to app/bundles/SmsBundle/Api/TwilioApi.php

Search on the line 88

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

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

        try {

            $this->client->account->messages->sendMessage(
                $this->sendingPhoneNumber,
                $this->sanitizeNumber($number),
                $content
            );

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

and reemplace with this:

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

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

        try {

/********************** Add Support to Whatsapp ***************************/
	    $prefix = 'whatsapp:';

            $this->client->account->messages->sendMessage(
                $prefix.$this->sendingPhoneNumber,
                $prefix.$this->sanitizeNumber($number),
                $content
            );

/*******se adiciono $prefix a $this->sanitizeNumber($number)****************/

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

also remmber the international format of the contact phones, mautic format the phone by defect with US format, by this add automatically +1 to all phone number before send to twilio. This is because this lines

search the line 74

    protected function sanitizeNumber($number)
    {
        $util   = PhoneNumberUtil::getInstance();
        $parsed = $util->parse($number, 'US');

        return $util->format($parsed, PhoneNumberFormat::E164);
    }

change to the country you want to send whatsapp message the line

$parsed = $util->parse($number, 'US');

for example for Peru

$parsed = $util->parse($number, 'PE')

and with this mautic automatically put +51 to the contact mobile phone that are store in mautic. Also remember put the sending phone number in the twilio plugin with no spaces and with international format.

With this change my mautic can send trought twilio service, as is send an sms. Remember you need to have an Twilio acount and a sender number activate.

in reply to this message:

there is a change on twilio

The weebhook need to be updated

The FROM parameter now its not a number, twilio give you an ID for it
The BODY parameter change to CONTENSID, twilio give you an ID for it
And you need to add the CONTENTVARIABLES parameter to send the variables you put in your message.