Preface
LINE Emoji refers to the LINE emoji sets that can be used in the LINE App, which are divided into general (free) and paid emoji sets. Using LINE Emoji in messages can make users feel more when reading. But how should developers correctly send and receive these LINE Emojis?
This article will guide how to correctly send and receive LINE emoji sets (LINE Emoji) through Golang example code.
Slides:
Sample Code
https://github.com/kkdai/linebot-emoji
Run an actual example (demo)
- Add this official account:
- Send an emoji at will, and you will see the chatbot reply to you in three ways. Respectively:
- Send in the old Emoji delivery method
- Send in the new Emoji delivery method
- Add the user's Emoji and send it through the new Emoji
New API Description:
Use LINE emoji in messages (2020/April)
Now, you don't need to do unicode conversion to send LINE emoji in text messages. You can directly add the relevant LINE emoji number in the API to achieve this, which makes development more convenient and flexible.
You can refer to the new API: Text message or refer to the new announcement: [Updated] Messaging API update for April 2020
Getting LINE emoji information from the text object of a webhook event (2020/May)
After providing the new sending API in April, the Webhook in May also provides new Webhook information that allows the chatbot to effectively handle LINE Emoji. Through emojis, you can get detailed information of all LINE Emojis that appear in the message as follows:
You can refer to the new API: Text message webhook or refer to the new announcement: Messaging API update for May 2020.
Develop a LINE Emoji Echo Bot using Golang:
Next, we will use Golang to develop an Echo Bot based on the functions provided by https://github.com/line/line-bot-sdk-go. That is, a chatbot that will reply according to the text spoken by the user. But unlike the general Echo Chatbot, this Echo Bot will return the LINE Emoji passed by the user, so it needs to have the following functions:
- Capture the
emojisinformation in the Webhook text Message. - Replace
(xxx)in the original text (as an expression of the emoji, for example, (heart) is a heart) with$. - Combine the text and expressions that need to be replied to the user, there are two things to pay attention to:
- Add
emojiinformation to the user's reply text, and the relevantindexinformation needs to be adjusted. - Before sending
emoji, pay attention to whether it exists in thesendable LINE Emoji list. Because some emoji packs need to be paid for, only free ones and those provided by LINE official can be sent through the chatbot. For details, please see: LINE Sendable LINE Emoji List.
- Add
Next, the relevant process will be explained through the source code description:
Capture the emojis information in the Webhook text Message:
After receiving the text message webhook, there will be more data to read the msg.Emojis array structure. Each cell of data may be similar to the following data format:
According to the above received data, the following information can be found:
-
text: isHello, world! (love), where Emoji will also be converted into text in the original text as(love). -
emojis: is all the Emojis that appear, and provides the startingindexandlength. Whereindexis zero-based,lengthincludes().
According to the above information, if you need to remove the emoji to do Language Understanding (semantic analysis). You need to follow these steps:
-
Hello, world! (love)Refer to theEmojisdata to remove(love)and recombine it asHello, world!.
Send emojis through the new LINE Emoji API:
According to the above example code, you can know that the data format to be sent is as follows:
-
text:"$%s Hello \n, this is the new way to send Emoji."Where$is the position where you want to send LINE Emoji, and%sis the message to be combined with the message from the user. -
AddEmoji: is the LINE Emoji you want to pass, whereProductIDandEmojiIDneed to be queried in LINE Sendable LINE Emoji List. This example is the example of Brown (ProdctID=5ac1bfd5040ab15980c9b435,EmojiID=086)
According to this method, the completed Echo Bot can only completely transmit the user's text, but the LINE Emoji sent by the user cannot be displayed correctly. (It will be displayed as (love) or (heart)).
Complete return of the LINE Emoji message sent by the user
So how to return it completely? It needs to go through the following three major steps:
- Read the original user's message and start parsing LINE Emoji
- Replace the original message with
(brown)with$. Used as needed to send later. - Include the original text sent by the user in the text to be sent by the chatbot. And modify the position of all
indexinEmojis(the position ofindexis changed due to some other text of the robot).
Next, I will share the relevant conversion methods:
The main function of this part is to convert the message with LINE Emoji sent by the user to $. (e.g. Hello, world! (love) –> Hello, world! $). It mainly uses the way of string decomposition, that is, string(msgArray[:index]), "$", string(msgArray[index+v.Length:] to decompose the text and recombine it.
Write relevant text conversion test cases:
Because there may be many types encountered (e.g. hi, _(brown)_, yo (love) (love)), so in order to prevent possible problems. We have also prepared a lot of test code. Here are some of them, and for a complete test example, it is recommended to go to https://github.com/kkdai/LineBot-emoji/blob/master/tool_test.go to view.
This test code only tests the test cases that appear once or not at all in the entire text. By writing a complete enough test case, the stability of the entire chatbot can be better, and some foreseeable errors can be found without deployment.
Check if it is a "sendable emoji service":
Because some emoji packs need to be paid for, only free ones and those provided by LINE official can be sent through the chatbot. For details, please see: LINE Sendable LINE Emoji List.
If you send non-free LINE Emoji data to the server, you will receive an error message. So this part is to check by organizing the data.
Complete conversion and check process:
Quickly explain the relevant application code:
- (5)
workMsg := ReplaceEmoji(msg.Text, msg.Emojis): Replace the user's text received with$. - (17)
lastLength = lastLength + v.Length: What is worth sharing here is that the calculation of the address of$after the conversion needs to be calculated through the length of the original Emoji. This can calculate theindexwhere each emoji replaces the text$.
Summary and Future Work
Through the API of LINE Emoji, developers can handle the sending of emojis more intuitively and simply. It also makes the interaction between the chatbot and the user closer. However, if you need to return the text (plus emojis) sent by the user, you need a lot of extra processing.
After that, there may be the following related processing for these parts:
- Natural language pre-processing: If you want to do semantic analysis (Language Understanding) for the text replied by the user, you must define the processing method for LINE Emoji. Developers can choose to ignore it completely, or put it into the analyzer.
- Interact with emojis: Users will use emojis, which will have relative meanings. In some cases, you can judge the user's emotions through emojis. This is also a very big subject.







Top comments (0)