title: Python asyncio with Slack bot
body: <p>I'm trying to make a simple Slack bot using asyncio, largely using the example <a href="https://stackoverflow.com/questions/30460929/how-to-create-an-event-loop-with-some-coroutines-running-forever">here</a> for the asyncio part and <a href="https://medium.com/@julianmartinez/how-to-write-a-slack-bot-end-to-end-d6a8542c854b#.2d7gty2o1" rel="nofollow noreferrer">here</a> for the Slack bot part. </p>
  
  <p>Both the examples work on their own, but when I put them together it seems my loop doesn't loop: it goes through once and then dies. If <code>info</code> is a list of length equal to 1, which happens when a message is typed in a chat room with the bot in it, the coroutine is supposed to be triggered, but it never is. (All the coroutine is trying to do right now is print the message, and if the message contains "/time", it gets the bot to print the time in the chat room it was asked in). Keyboard interrupt also doesn't work, I have to close the command prompt every time.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import asyncio
  from slackclient import SlackClient
  import time, datetime as 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 is ' + 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()
  </code></pre>
  
  <p>I think it's the loop part that's broken, since it never seems to get to the coroutine. So maybe a shorter way of asking this question is what is it about my try: statement that prevents it from looping like in the asyncio example I followed? Is there something about <code>sc.rtm_connect()</code> that it doesn't like?</p>
  
  <p>I'm new to asyncio, so I'm probably doing something stupid. Is this even the best way to try and go about this? Ultimately I want the bot to do some things that take quite a while to compute, and I'd like it to remain responsive in that time, so I think I need to use asyncio or threads in some variety, but I'm open to better suggestions.</p>
  
  <p>Thanks a lot,
  Alex</p>