diff --git a/examples/kitchensink/server.go b/examples/kitchensink/server.go index 168498b7..740a8fe2 100644 --- a/examples/kitchensink/server.go +++ b/examples/kitchensink/server.go @@ -28,6 +28,7 @@ import ( "github.com/line/line-bot-sdk-go/v8/linebot/messaging_api" "github.com/line/line-bot-sdk-go/v8/linebot/webhook" + "github.com/line/line-bot-sdk-go/v8/util" ) func main() { @@ -548,6 +549,33 @@ func (app *KitchenSink) handleText(message *webhook.TextMessageContent, replyTok } log.Printf("status code: (%v), x-line-request-id: (%v), error response: (%v)", resp.StatusCode, resp.Header.Get("x-line-request-id"), errorResponse) } + case "emoji": + message := "Hello, $ hello こんにちは $, สวัสดีครับ $" + emojiIndexes := util.FindDollarSignIndexInUTF16Text(message) + emojis := []messaging_api.Emoji{} + for _, index := range emojiIndexes { + emojis = append(emojis, messaging_api.Emoji{ + Index: int32(index), + ProductId: "5ac1bfd5040ab15980c9b435", + EmojiId: "001", + }) + } + result, _, err := app.bot.ReplyMessageWithHttpInfo( + &messaging_api.ReplyMessageRequest{ + ReplyToken: replyToken, + Messages: []messaging_api.MessageInterface{ + messaging_api.TextMessage{ + Text: message, + Emojis: emojis, + }, + }, + }, + ) + if err == nil { + log.Printf("Sent reply: %v", result) + } + log.Printf("Sent reply: %v %v", result, err) + return err default: log.Printf("Echo message to %s: %s", replyToken, message.Text) if _, err := app.bot.ReplyMessage( diff --git a/util/find_dollar_sign.go b/util/find_dollar_sign.go new file mode 100644 index 00000000..470966bb --- /dev/null +++ b/util/find_dollar_sign.go @@ -0,0 +1,25 @@ +package util + +import "unicode/utf16" + +/* +When you want to send a text message with emoji, you need to add +$ in the text, and identify the index of the $ in the text. + +FindDollarSignIndexInUTF16Text helps you to find the index of the $ in the text. +*/ + +type CharInUTF16 uint16 + +const dollarSign CharInUTF16 = 36 + +func FindDollarSignIndexInUTF16Text(text string) (indexes []int32) { + encoded := utf16.Encode([]rune(text)) + for i, unit := range encoded { + + if unit == uint16(dollarSign) { + indexes = append(indexes, int32(i)) + } + } + return indexes +} diff --git a/util/find_dollar_sign_test.go b/util/find_dollar_sign_test.go new file mode 100644 index 00000000..0a3be237 --- /dev/null +++ b/util/find_dollar_sign_test.go @@ -0,0 +1,13 @@ +package util + +import ( + "testing" +) + +func TestFindDollarSignIndexInUni16Text(t *testing.T) { + text := "Hello, $ hello こんにちは $, สวัสดีครับ $" + indexes := FindDollarSignIndexInUTF16Text(text) + if len(indexes) != 3 { + t.Errorf("Expected 3, but got %d", len(indexes)) + } +}