我這邊群組團購訊息會用到很多LINE emoji,這邊會將訊息跟LINE emoji做處理
前面Day 09 回覆emoji訊息有提到Android上還接收不到emoji參數,這邊我會從windows版上去操作。
接收到的message event中,emojis包含emojiId、index、length跟productId,而訊息會變成(文字)形式,這跟我們要發送的訊息不同,這邊也會針對這邊做處理
{"message": {"emojis": [{"emojiId": "001", "index": 0, "length": 3, "productId": "5ac21ae3040ab15980c9b440"}, {"emojiId": "002", "index": 3, "length": 3, "productId": "5ac21a8c040ab15980c9b43f"}, {"emojiId": "003", "index": 6, "length": 3, "productId": "5ac21a13031a6752fb806d57"}], "id": "14814002850836", "text": "(A)(B)(C) 123", "type": "text"}, "mode": "active", "replyToken": "c53e4118cd0d41ce93a974f7886374bf", "source": {"groupId": "群組id", "type": "group", "userId": "使用者id"}, "timestamp": 1632694923794, "type": "message"}
這便透過emoji的index與length將有()的文字的替換為$
def handle_emoji_text(text,emojis):
lastLength=0
for emoji in emojis:
index =emoji.index-lastLength
text="%s%s%s"%(text[:index],'$',text[index+emoji.length:])
lastLength = lastLength + emoji.length-1
return text
message=TextSendMessage(handle_emoji_text(event.message.text,event.message.emojis))
將移動過的emoji index修正
def handle_emoji(emojis):
lastLength=0
for emoji in emojis:
emoji.index =emoji.index-lastLength
lastLength = lastLength + emoji.length-1
return emojis
message=TextSendMessage( handle_emoji_text(event.message.text,event.message.emojis),emojis=handle_emoji(event.message.emojis))