I want to request, if it is possible to encode the response from chatterbot with utf-8, because in the moment it is an object as far as I know and that is problematic for the use with special characters and umlauts (ä,ö,ü).
Python is giving me the following error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 781, in collector
callback(item)
File "chatterbot.py", line 42, in handle
bot.sendMessage(chat_id, str(response))
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 11: ordinal not in range(128)
_I am using Chatterbot with the Telegram- API_
The code for this is:
#!/usr/bin/env python
# coding: utf8
...
def handle(msg):
global chat_id
chat_id = msg['chat']['id']
command = msg['text'].encode('utf8') #Incoming text from telegramuser
response = susan.get_response(command) #response from chatterbot to the given text
bot.sendMessage(chat_id, str(response)) #trys to reply response to the telegramchat
I believe your unicode header is incorrect. It should be # -*- coding: utf-8 -*-
I tried it with both versions of the headers, but for that it doesnt make any difference.
ChatterBot is able to handle unicode values correctly. You can pass it non-encoded data and it should be able to process it properly (you'll just need to decode the output that it returns).
Bellow is one of ChatterBot's tests from tests/test_chatbot.py, this is just a simple check that a unicode response can be passed in.
def test_get_response_unicode(self):
"""
Test the case that a unicode string is passed in.
"""
response = self.chatbot.get_response(u'سلام')
self.assertGreater(len(response.text), 0)
This test passes in both Python 2.7 and 3.x. It also demonstrates that ChatterBot currently _can_ take unicode input without issue. Based on this, I believe the unicode error you are encountering may be in your own code, rather than ChatterBot's (I could be wrong, but this is pretty strong evidence).
You are right, there was a issue in my encoding. Thanks for your help!
You can close this issue.
@ThomasKoscheck can you post how you solved your problem i am facing the same issue, i cant use arabic with chatterbot
@mouhsinelonly This is my solution:
def on_chat_message(self, msg):
global chat_id
content_type, chat_type, chat_id = telepot.glance(msg)
**reload(sys)**
**sys.setdefaultencoding("utf-8")**
try:
chat_id = msg['chat']['id']
firstname = msg['from']['first_name']**.encode('utf8')**
username = msg['from']['username']**.encode('utf8')**
The bold ones are the important ones.
edited version
def on_chat_message(self, msg):
global chat_id
content_type, chat_type, chat_id = telepot.glance(msg)
reload(sys)
sys.setdefaultencoding("utf-8")
try:
chat_id = msg['chat']['id']
firstname = msg['from']['first_name'].encode('utf8')
username = msg['from']['username'].encode('utf8')
i solved another way by using python3 and exporting the envitrenment
encoding for python . thank you
On Mon, Aug 21, 2017, 4:52 AM Mallikarjunarao Kosuri <
[email protected]> wrote:
correct format
def on_chat_message(self, msg):
global chat_id
content_type, chat_type, chat_id = telepot.glance(msg)
reload(sys)
sys.setdefaultencoding("utf-8")try: chat_id = msg['chat']['id'] firstname = msg['from']['first_name']*.encode('utf8')* username = msg['from']['username']*.encode('utf8')*—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/gunthercox/ChatterBot/issues/509#issuecomment-323623412,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAfal6TMhPRzqdigGQl-lm9ykVUb9r4-ks5saNSzgaJpZM4LPcEB
.>
Ù…ØØ³Ù† بخيش
مدير قسم البرمجة بمركز التعليم عن بعد ،كلية العلوم الشرعية ،مسقط ، عمان
المØÙ…ول : +96890164489
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
You are right, there was a issue in my encoding. Thanks for your help!
You can close this issue.