首页 > 解决方案 > How encode in a right why emojis using node request module?

问题描述

I'm using request module in order to send message to a Telegram bot, everything is fine, except the way I'm display emojis, which don't get printed well

Post request:

request.post({
            url: `${this.getApiURL()}/${apiName}`,
            formData: payload,
            headers: {
                'Content-Type': 'application/json',
                'Charset': 'utf-8',
            }
        }, (err, resp, body) => {});

The payload contains a text message with some emojis like:

const _emojis = {
            throwingAKiss: '\xF0\x9F\x98\x98',
            tearsOfJoy: '\xF0\x9F\x98\x82',
            smirkingFace: '\xF0\x9F\x98\x8F'
        }

but I'm displaying this symbol ð

标签: javascriptnode.jsrequesttelegram-botemoji

解决方案


As a workaround you may have success with using a different unicode notation, i.e.:

const smiley = "\u{1F604}";

For a list of codes you there are emoji tables out there.

Additional, it might ease a bit development overhead by using libaries such as node-emoji (which uses raw emojis under the hood through omnidan/node-emoji json file.


推荐阅读