Skip to content

Commit

Permalink
Find dollar sign in a text string for adding emoji (#458)
Browse files Browse the repository at this point in the history
* add FindDollarSign function to find emoji index in text string

* add FindDollarSign function to find emoji index in text string

* move function to util folder/package and make dollar sign more readable

* rename function
  • Loading branch information
punkzberryz authored Jun 19, 2024
1 parent d576495 commit 955fcb7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/kitchensink/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions util/find_dollar_sign.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions util/find_dollar_sign_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 955fcb7

Please sign in to comment.