asynchronous - Python asyncio with Slack bot -


i'm trying make simple slack bot using asyncio, largely using example here asyncio part , here slack bot part.

both examples work on own, when put them seems loop doesn't loop: goes through once , dies. if info list of length equal 1, happens when message typed in chat room bot in it, coroutine supposed triggered, never is. (all coroutine trying right print message, , if message contains "/time", gets bot print time in chat room asked in). keyboard interrupt doesn't work, have close command prompt every time.

here code:

import asyncio slackclient import slackclient import time, datetime dt  token = "my token" sc = slackclient(token)  @asyncio.coroutine def read_text(info):     if 'text' in info[0]:         print(info[0]['text'])         if r'/time' in info[0]['text']:             print(info)             resp = 'the time ' + dt.datetime.strftime(dt.datetime.now(),'%h:%m:%s')             print(resp)             chan = info[0]['channel']             sc.rtm_send_message(chan, resp)   loop = asyncio.get_event_loop() try:     sc.rtm_connect()     info = sc.rtm_read()     if len(info) == 1:         asyncio.async(read_text(info))     loop.run_forever()  except keyboardinterrupt:     pass finally:     print('step: loop.close()')     loop.close() 

i think it's loop part that's broken, since never seems coroutine. maybe shorter way of asking question try: statement prevents looping in asyncio example followed? there sc.rtm_connect() doesn't like?

i'm new asyncio, i'm doing stupid. best way try , go this? want bot things take quite while compute, , i'd remain responsive in time, think need use asyncio or threads in variety, i'm open better suggestions.

thanks lot, alex

i changed following , worked:

import asyncio slackclient import slackclient import time, datetime dt  token = "my token"     sc = slackclient(token)  @asyncio.coroutine def listen():     yield asyncio.sleep(1)     x = sc.rtm_connect()     info = sc.rtm_read()     if len(info) == 1:         if 'text' in info[0]:             print(info[0]['text'])             if r'/time' in info[0]['text']:                 print(info)                 resp = 'the time ' + dt.datetime.strftime(dt.datetime.now(),'%h:%m:%s')                 print(resp)                 chan = info[0]['channel']                 sc.rtm_send_message(chan, resp)      asyncio.async(listen())   loop = asyncio.get_event_loop() try:     asyncio.async(listen())     loop.run_forever()  except keyboardinterrupt:     pass finally:     print('step: loop.close()')     loop.close() 

not entirely sure why fixes it, key things changed putting sc.rtm_connect() call in coroutine , making x = sc.rtm_connect(). call listen() function @ end, appears makes loop forever, since bot doesn't respond if take out. don't know if way sort of thing supposed set up, appear continue accept commands while it's processing earlier commands, slack chat looks this:

me [12:21 am]  /time  [12:21]  /time  [12:21]  /time  [12:21]  /time  testbotbot [12:21 am]  time 00:21:11  [12:21]  time 00:21:14  [12:21]  time 00:21:16  [12:21]  time 00:21:19 

note doesn't miss of /time requests, if weren't doing stuff asynchronously. also, if trying replicate you'll notice slack brings built in command menu if type "/". got around typing space in front.

thanks help, please let me know if know of better way of doing this. doesn't seem elegant solution, , bot can't restarted after use cntrl-c keyboard interrupt end - says

task exception never retrieved future: <task finished coro=<listen() done, defined @ asynctest3.py:8> exception=attributeerror("'nonetype' object has no attribute 'recv'",)> traceback (most recent call last):   file "c:\users\dell-f5\appdata\local\programs\python\python35-32\lib\asyncio\tasks.py", line 239, in _step     result = coro.send(none)   file "asynctest3.py", line 13, in listen     info = sc.rtm_read()   file "c:\users\dell-f5\envs\sbot\lib\site-packages\slackclient\_client.py", line 39, in rtm_read     json_data = self.server.websocket_safe_read()   file "c:\users\dell-f5\envs\sbot\lib\site-packages\slackclient\_server.py", line 110, in websocket_safe_read     data += "{0}\n".format(self.websocket.recv()) attributeerror: 'nonetype' object has no attribute 'recv' 

which guess means it's not closing websockets nicely. anyway, that's annoyance, @ least main problem fixed.

alex


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -