How to send an emoji in Twilio Whatsapp Template

294 Views Asked by At

I have a WhatsApp template on Twilio and I want you to have some emojis but I don't know how to do it. How can I add them? I want to do the same on a webhook.

public function SendMessage(){
    $sid = getenv("TWILIO_ACCOUNT_SID");
    $token = getenv("TWILIO_AUTH_TOKEN");
    $twilio = new Client($sid, $token);

    $message = $twilio->messages
        ->create("whatsapp:+14155238886", // to
            [
                "from" => "whatsapp:+15005550006",
                "body" => "this is a message with emoji"
            ]
        );

    print($message->sid);
}
public function actionWebHook()
{
    $from = $_POST['From'];
    $body = $_POST['Body'];

    $response = $_POST;
    if(is_array($response)) {
        $response = json_encode($response, true);        }

    $response = new MessagingResponse();
    $response->message("Emoji here!Su *mensaje* ha sido recibido y  fue: ".$body);

    print $response;
    exit();
}
1

There are 1 best solutions below

0
Ari On

Answer

In the WhatsApp Template you can directly add the emoji characters into the "body" parameter of the message. For example, if you want to add a smiley face emoji, you can do it as follows:

$message = $twilio->messages
    ->create("whatsapp:+14155238886", // to
        [
            "from" => "whatsapp:+15005550006",
            "body" => "this is a message with emoji "
        ]
    );

In the Webhook Response: Similarly, you can add emoji characters to the response message. For example:

$response = new MessagingResponse();
$response->message("Emoji here!  Su *mensaje* ha sido recibido y fue: ".$body);

Emojis can be copied from an emoji keyboard or an online emoji list.

Addendum

However, you may want to use a more sophisticated mechanism, such as sentiment analysis, such that it outputs emojis in response to the sentiment detected.

I would use a combination of Python and PHP in this case.

Set Up a Python Sentiment Analysis Script

Write a Python script that takes text input and outputs sentiment scores. Here's a simple example using TextBlob

from textblob import TextBlob

def analyze_sentiment(text):
    testimonial = TextBlob(text)
    return testimonial.sentiment.polarity

This function returns a polarity score between -1 (negative) and 1 (positive).

Integrate Sentiment Analysis with Your PHP Code

Call the sentiment analysis tool/API from your PHP code and get the sentiment score. For a Python script, you can use exec() or a similar function in PHP. For an API, you'll make an HTTP request.
$sentimentScore = exec('python sentiment_analysis.py "'. $body .'"');

Choose an Emoji Based on Sentiment Score

Based on the sentiment score, choose an appropriate emoji. For example:
if ($sentimentScore > 0.5) {
    $emoji = ''; // Positive
} elseif ($sentimentScore < -0.5) {
    $emoji = ''; // Negative
} else {
    $emoji = ''; // Neutral
}

Include the Emoji in Your Message

Add the chosen emoji to your message body.
$response->message($emoji . " Su *mensaje* ha sido recibido y fue: " . $body);