[PYTHON] About Japanese support of cometchat

Do you know cometchat?

cometchat is a chat API & message SDK that allows you to easily set up a chat function on your website.

I used this comet at pro (cometch at's more flexible service) API to add chat functionality to my site. (Implemented in python (Django) based on here) But after completion, a tragedy will occur.

I can't send Japanese text with cometchat. .. .. .. My site is in Japanese

The site has become international.

The official document says, "Japanese is also supported!" Actually, when I try to send a message in Japanese, I get the following error.

UnicodeEncodeError: 'latin-1' codec can't encode characters 
in position 90-92: Body ('Ah ah') is not valid Latin-1. 
Use body.encode('utf-8') if you want to send it encoded in UTF-8.

Use body.encode ('utf-8') ← The above error tells me that I should try this, If you try, ERR_BAD_REQUEST will be returned.

{"error":{"code":"ERR_BAD_REQUEST","details":{"receiver":["The
 receiver field is required."],"receiverType":["The receiver 
type field is required."]},"message":"Failed to validate the 
data sent with the request."}}

Solution

When sending a message. Convert to byte and convert to hexadecimal character string.

text = "Message you want to send"
text.encode().hex() 
# 'e98081e3828ae3819fe38184e383a1e38383e382bbe383bce382b8'

When receiving a message. Converts a hexadecimal string to bytes and bytes to str.

bytes.fromhex(text).decode()
# "Message you want to send"

Recommended Posts

About Japanese support of cometchat
About Japanese path of pyminizip
About Japanese fonts of matplotlib (for Mac)
About all of numpy
About assignment of numpy.ndarray
About MultiIndex of pandas
About variable of chainer
Japanese localization of Pycharm
About max_iter of LogisticRegression () of scikit-learn
Japanese display of matplotlib, seaborn
About the garbled Japanese part of pandas-profiling in Jupyter notebook
Japanese localization setting of PyCharm
About the ease of Python
About various encodings of Python 3
About all of numpy (2nd)
About cost calculation of MeCab
About approximate fractions of pi
Japanese translation of sysstat manual
About the components of Luigi
Japanese translation of Linux manual
About HOG output of Scikit-Image
About the features of Python
About data management of anvil-app-server
Japanese translation of the e2fsprogs manual
About the return value of pthread_mutex_init ()
About pyenv's support for Anaconda / Miniconda
About the return value of the histogram.
About the basic type of Go
Topic extraction of Japanese text 1 Basics
About the upper limit of threads-max
About circular crossover of genetic algorithms
Japanese translation of the man-db manual
Appropriate Japanese translation of pytorch tensor_tutorial
About the behavior of yield_per of SqlAlchemy
About import error of PyQt5.QtWidgets (Anaconda)
About the size of matplotlib points
About color halftone processing of images
Japanese translation of the util-linux manual
About the basics list of Python basics
Japanese translation of the iproute2 manual