-1

I'm using the reddit-flair-bot for my subreddit.

If you're not familiar with this, it's a python script that handles incoming PMs from users, where it takes the subject as the flair class and the content of the PM as the flair text.

That's just a bit of context, but here's the important part - by default, you cannot send a blank PM on reddit. This means users must have text when sending the PM (meaning they must also have text next to their name).

We've recently found a workaround to this issue, where you can insert a blank character instead, and it will be identified as a character (so the PM will send) but it will show up as nothing next to your name. I'm talking about an actual blank character and not a blank space (alt+0143 and alt+0173 fulfill this criteria).

The problem is that the bot uses PRAW, and PRAW can't seem to detect the blank unicode character. So the script doesn't run properly.

It comes with the error:

  UnicodeEncodeError: 'ascii' codec can't encode characters in position 19-20: ordinal not in range(128)

Is there any way to work around this? I hope I've explained my situation clearly.

antonlab
  • 323
  • 3
  • 5
  • 11
  • When including a Python error message it's generally useful to show the entire call stack. That makes it easier to tell where the error is coming from. – Mark Ransom Oct 27 '15 at 02:04
  • @MarkRansom Something like [this?](https://i.imgur.com/24MGL08.png) – antonlab Oct 27 '15 at 02:20
  • Exactly, although it would be better if you copy/pasted directly into the question. It indicates that the error occurs in `logfile.write`, which tells me this is a text mode file where you have not specified an encoding. – Mark Ransom Oct 27 '15 at 02:40
  • 1
    It probably would be easier to not have users send a blank space to remove the flair, but instead to have them send a specific message (eg `clear flair`), simply because trying to tell someone how to enter a blank space would be difficult. – Pokechu22 Oct 27 '15 at 14:43
  • @Pokechu22 Great idea! Thanks! – antonlab Oct 27 '15 at 22:38

1 Answers1

0

ALT+0143 is u'\u008f'

ALT+0143 is u'\u00ad' # soft hypen

Use the above unicode code points to represent the characters in your code and then encode them using 'utf-8' before sending them.

>>> aa = u'\u008f'
>>> aa_str = aa.encode('utf-8')
>>> aa_str
'\xc2\x8f'

Now use aa_str for transmission.

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208