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:
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.