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();
}
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.
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.
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).
$sentimentScore = exec('python sentiment_analysis.py "'. $body .'"');
if ($sentimentScore > 0.5) {
$emoji = '😊'; // Positive
} elseif ($sentimentScore < -0.5) {
$emoji = '😞'; // Negative
} else {
$emoji = '😐'; // Neutral
}
$response->message($emoji . " Su *mensaje* ha sido recibido y fue: " . $body);