0

I have made a Slack bot using slackclient 1.3.2 and Python 3.5 in a virtualenv. I'm running this on Windows 7. One of the functions sends a files from a directory when requested. This function seems to work fine when sending files whose names and paths contain only ascii, but it doesn't do anything when I ask it to send a file with an accent in the name.

Here is the relevant part of the code:

import asyncio, requests
from slackclient import SlackClient

token = "MYTOKEN"
sc = SlackClient(token)

@asyncio.coroutine
def sendFile(sc, filename, channels):
    print(filename)
    f = {'file': (filename, open(filename, 'rb'), 'text/plain', {'Expires':'0'})}
    response = requests.post(url='https://slack.com/api/files.upload', 
                             data= {'token': token, 'channels': channels, 'media': f},
                             headers={'Accept': 'application/json'}, files=f)

@asyncio.coroutine
def sendExample(sc, chan, user, instructions):
    path1 = 'D:/Test Files/test.txt' #note: just ascii characters
    path2 = 'D:/Test Files/tést.txt' #note: same as path1 but with accent over e in filename

    print('instructions: ', instructions)
    if instructions[0] == 'path1':
        sc.rtm_send_message(chan, 'Sending path1!')
        asyncio.async(sendFile(sc, path1, chan))

    if instructions[0] == 'path2':
        sc.rtm_send_message(chan, 'Sending path2!')
        asyncio.async(sendFile(sc, path2, chan))

@asyncio.coroutine
def listen():
    x = sc.rtm_connect()
    while True:
        yield from asyncio.sleep(0)  

        info = sc.rtm_read()
        if len(info) == 1:
            if 'text' in info[0]:
                print(info[0]['text'])

                if r'send' in info[0]['text'].lower().split(' '):
                    chan = info[0]['channel']
                    user = info[0]['user']
                    instructions = info[0]['text'].lower().split(' ')[1:]
                    asyncio.async(sendExample(sc, chan, user, instructions))

def main():
    print('Starting bot.')
    loop = asyncio.get_event_loop()
    asyncio.async(listen())
    loop.run_forever()

if __name__ == '__main__':
    main()

D:/Test Files/test.txt and D:/Test Files/tést.txt are identical text files, just one has an accent above the e in the file name.

Here is what it looks like from the Slack interface:

Slack point of view

As you can see, when I request path1 (test.txt) it works, but when I request path2 (tést.txt) it says it is sending something but doesn't seem to do anything.

The command line output looks the same for both of the files:

>python testSlackSend.py
Starting bot.
send path1
instructions:  ['path1']
D:/Test Files/test.txt
Sending path1!

send path2
instructions:  ['path2']
D:/Test Files/tést.txt
Sending path2!

I guess I can just rename all the files to get rid of the accents, but that's not really ideal. Is there a more elegant way to solve this problem?

If it will help with the problem I could migrate the whole thing to slackclient 2.x, but that seems like a lot of work that I'd rather not do if it's an option.

Alex S
  • 4,726
  • 7
  • 39
  • 67
  • Just a hunch, but might be a problem with the character encoding. Would double check that you have everything coded in UTF-8 as Slack expects it. – Erik Kalkoken Aug 17 '19 at 21:13
  • Btw. Any reason you are uploading the file by yourself with requests instead of using slackclient? – Erik Kalkoken Aug 17 '19 at 21:18
  • Ok thanks, do you mean is the code itself in UTF-8? Or the file names? Not sure how to modify the file name encoding. I originally wrote this code a year or two ago, and I'm afraid I can't remember why I used requests. I'll try switching to the slackclient function. – Alex S Aug 19 '19 at 14:07
  • I mean the code itself and every string it handles. Check out this great guide for details: https://stackoverflow.com/questions/41542613/utf-8-string-in-python-2-and-3 – Erik Kalkoken Aug 19 '19 at 14:15

0 Answers0