Skip to content

Commit 955fcb7

Browse files
authored
Find dollar sign in a text string for adding emoji (#458)
* 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
1 parent d576495 commit 955fcb7

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

examples/kitchensink/server.go

+28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
3030
"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
31+
"github.com/line/line-bot-sdk-go/v8/util"
3132
)
3233

3334
func main() {
@@ -548,6 +549,33 @@ func (app *KitchenSink) handleText(message *webhook.TextMessageContent, replyTok
548549
}
549550
log.Printf("status code: (%v), x-line-request-id: (%v), error response: (%v)", resp.StatusCode, resp.Header.Get("x-line-request-id"), errorResponse)
550551
}
552+
case "emoji":
553+
message := "Hello, $ hello こんにちは $, สวัสดีครับ $"
554+
emojiIndexes := util.FindDollarSignIndexInUTF16Text(message)
555+
emojis := []messaging_api.Emoji{}
556+
for _, index := range emojiIndexes {
557+
emojis = append(emojis, messaging_api.Emoji{
558+
Index: int32(index),
559+
ProductId: "5ac1bfd5040ab15980c9b435",
560+
EmojiId: "001",
561+
})
562+
}
563+
result, _, err := app.bot.ReplyMessageWithHttpInfo(
564+
&messaging_api.ReplyMessageRequest{
565+
ReplyToken: replyToken,
566+
Messages: []messaging_api.MessageInterface{
567+
messaging_api.TextMessage{
568+
Text: message,
569+
Emojis: emojis,
570+
},
571+
},
572+
},
573+
)
574+
if err == nil {
575+
log.Printf("Sent reply: %v", result)
576+
}
577+
log.Printf("Sent reply: %v %v", result, err)
578+
return err
551579
default:
552580
log.Printf("Echo message to %s: %s", replyToken, message.Text)
553581
if _, err := app.bot.ReplyMessage(

util/find_dollar_sign.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package util
2+
3+
import "unicode/utf16"
4+
5+
/*
6+
When you want to send a text message with emoji, you need to add
7+
$ in the text, and identify the index of the $ in the text.
8+
9+
FindDollarSignIndexInUTF16Text helps you to find the index of the $ in the text.
10+
*/
11+
12+
type CharInUTF16 uint16
13+
14+
const dollarSign CharInUTF16 = 36
15+
16+
func FindDollarSignIndexInUTF16Text(text string) (indexes []int32) {
17+
encoded := utf16.Encode([]rune(text))
18+
for i, unit := range encoded {
19+
20+
if unit == uint16(dollarSign) {
21+
indexes = append(indexes, int32(i))
22+
}
23+
}
24+
return indexes
25+
}

util/find_dollar_sign_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFindDollarSignIndexInUni16Text(t *testing.T) {
8+
text := "Hello, $ hello こんにちは $, สวัสดีครับ $"
9+
indexes := FindDollarSignIndexInUTF16Text(text)
10+
if len(indexes) != 3 {
11+
t.Errorf("Expected 3, but got %d", len(indexes))
12+
}
13+
}

0 commit comments

Comments
 (0)