jotadepicas jotadepicas - 1 month ago 14
Node.js Question

How to send emojis/emoticons with nodejs xmpp client on google chat/hangouts

I've implemented a Google chat bot using node-red xmpp client nodes.
Everything works ok, but I don't know how to make my bot to send me a smiley/emoji/emoticon/etc.

For example, I want to say to my bot "hey, thanks", and I'd like the bot response to be the "thumbs up" emoticon, the one that comes up when you type "(Y)":

enter image description here

But what I get is the literal string "(Y)" and not the graphical equivalent. Do I have to send it in unicode or something like that? I'm guessing the graphical interpretation is up to the chat client (I'm chatting from the hangouts widget inside the gmail inbox page).

Thanks!

Answer

Well, I finally got it.

As stated in this excellent article by Mathias Bynens, "Javascript has a Unicode problem".

I recommend reading the article, but in short, the thing is that depending on what unicode character you want to represent in javascript, you may need to actually use two characters together, which are called the "two surrogate halves" that form the actual character. This link also from the same author explains the formula to calculate these two halves based on the codepoint of the character.

However, I've found the site https://codepoints.net that lists all the characters and gives you the correct representation for different languages, Javascript and JSON among them.

For example, this is the page for the "thumbsup" character: https://codepoints.net/U+1F44D

There, under "Representations", if you click on "show more", you'll see many representations for different languages. For the thumbsup, that has the codepoint U+1F44D, the javascript representation is with this two surrogate halves: \uD83D\uDC4D

So, if you send the javascript string '\uD83D\uDC4D' to a unicode-enabled client, you should see the corresponding emoji.

Note: I ended up not implementing a Google chat bot (XMPP), but a Telegram bot using the Telegram API, but the key concepts are the same.

Comments