我试图让Python的curses
和{
我需要这两个while循环同时运行,并且能够彼此共享信息。当slackclient
接收到数据时,它需要显示在屏幕上。当用户输入文本时,它需要通过slackclient
发送出去。在
我已经能够编写slackclient
的测试运行,它使用asyncio
来监听stdin,并写出它从stdin听到的内容。但是我被困在这里不知道该怎么做。我的下一步可能是什么?在
以下是slackclient
/asyncio
测试代码:
import asyncio
import time
import sys
from slackclient import SlackClient
class SlackCLI(object):
def __init__(self):
self.token = 'xoxp-TOKEN-HIDDEN'
self.client = SlackClient(self.token)
self.channel = '#general'
def callback(self):
text = sys.stdin.readline()
print('[*] sending: {} to channel {}'.format(text, self.channel))
print(self.client.api_call('chat.postMessage', channel='#general',
text='{}'.format(text),
username='pybot123', icon_emoji=':robot_face:'))
@asyncio.coroutine
def run(self):
if self.client.rtm_connect():
while True:
last = self.client.rtm_read()
if last:
try:
text = last[0]['text']
chan = last[0]['channel']
print('[!] {}: {}'.format(chan, text))
except:
pass
yield from asyncio.sleep(1)
else:
print('connection failed')
def main(self):
loop = asyncio.get_event_loop()
loop.add_reader(sys.stdin, self.callback)
loop.run_until_complete(self.run())
a = SlackCLI()
a.main()
目前没有回答
相关问题 更多 >
编程相关推荐