butterfield
butterfield copied to clipboard
Support continous output from a subprocess/handler
I wrote an example handler for !ping example.com. The only way I could get it to actually return the output back to slack while still running was to add asyncio.sleep(). I'm thinking it would be better to handle the continous output from within the framework?
@asyncio.coroutine def ping(bot, message: 'message'): #yield from bot.post( # message['channel'], # message['text'] #) if 'text' not in message: return
if message['text'].startswith( '!ping' ):
match = re.findall(r"!ping( .*)?", message['text'])
if not match:
return
pingtarget = match[0].strip()
#yield from b.post(message['channel'], "ping "+pingtarget)
if re.search(r'\|',pingtarget):
# assuming slack did something weird with the "url"
# '<http://domain.com|domain.com>'
match = re.findall(r'\<http:\/\/(.+?)\|.*\>',pingtarget)
pingtarget = match[0]
yield from b.post(message['channel'], "Trying to ping "+pingtarget)
with subprocess.Popen(["ping", "-c1", pingtarget], stdout=subprocess.PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
yield from bot.post( message['channel'], line )
yield from asyncio.sleep(0.01)