1

Well, I came back to writing a bot for vk on python. It shows first signs of life and first obstacles.

One of these are smiles that may arrive in a private message or chat. I have a function that disassembles every incoming message into string using my own functions and json library.

#getting a message

def getmessage(out, count):
    payload['out']=str(out)
    payload['count']=str(count)
    return json.loads(requests.get(api+'messages.get', payload).text)    ['response']['items'][0]

#disassembling a message

def messageanalysis(message):
    m=message
    mes={
    'date':m['date'],
    'id':m['id'],
    'user_id':m['user_id'],
    'body':m['body']}
    return mes

So, then I .split() the string mes['body'] and compare every word from it to a set of templates and based on the result decide what to answer.

So just now I've received a smile in private messages and the program crashed, not knowing what to do with a .png file. It's interesting that unlike a regular attachment that has its own parameters in the object "message", received by vk server, the smile is directly integrated into the body of the message. What should I do to at least prevent the program from crashing because of smiles, or, which is better, make it recognize that a smile was sent?

====== Error:

Traceback (most recent call last):
  File "C:\BOT\Body.py", line 24, in <module>
    print('User_id: '+str(m['user_id'])+'\nMessage: '+str(m['body'])+'\n')
  File "C:\Users\Арсений\AppData\Local\Programs\Python\Python35-32    \lib\idlelib\PyShell.py", line 1344, in write
    return self.shell.write(s, self.tags)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 28-28:     Non-BMP character not supported in Tk
Warpony
  • 31
  • 5
  • Please, specify the error message and in which line is breaking – Jalo Nov 21 '16 at 13:31
  • Preparse the text and either strip smileys or map them to the text equivalent – Wboy Nov 21 '16 at 13:44
  • @Wboy done..... – Warpony Nov 21 '16 at 13:49
  • Piece of code you provided doesn't contain code where error emerges. – George Sovetov Nov 21 '16 at 14:39
  • You probably get text in UTF-8 and, when outputting, convert it in UCS-2. Try to use UTF-8 for output. Also, take a look at http://stackoverflow.com/questions/32442608/ucs-2-codec-cant-encode-characters-in-position-1050-1050. Anyway, I don't think you have to strip smileys. – George Sovetov Nov 21 '16 at 14:41
  • @GeorgeSovetov, I set json encoder to utf-8. Also if I open the smile directly from the message it appears to be a .png file – Warpony Nov 21 '16 at 16:22
  • Text is being encoded into UCS-2 when you try to output it. One of answers by link I included in previous message tells how to encode smileys to output them properly. – George Sovetov Nov 21 '16 at 16:26

1 Answers1

0

The code that is in your question is not the problem. You are using IDLE (based on the error message), which doesn't support displaying the characters outside of the "Basic Multilingual Plane" of Unicode (emojis are outside of the BMP). You should switch to using a command prompt, or replace the non-BMP characters with something else before printing them.