def __init__(self, token, plugin_filespec, proxy=None, debug=False): self._update_id = 0 self._client = AsyncioClient(token, proxy=proxy, debug=debug) self._plugins = [p(self._send_method) for p in PluginLoader(BotPlugin, plugin_filespec)] self._plugins.sort(key=lambda p: p.priority)
class TelegramBotService(object): def __init__(self, token, plugin_filespec, proxy=None, debug=False): self._update_id = 0 self._client = AsyncioClient(token, proxy=proxy, debug=debug) self._plugins = [p(self._send_method) for p in PluginLoader(BotPlugin, plugin_filespec)] self._plugins.sort(key=lambda p: p.priority) @asyncio.coroutine def run(self): for plugin in self._plugins: yield from plugin.startPlugin() try: while True: method = getUpdates() method.offset = self._update_id method.timeout = 30 updates = yield from self._client.send_method(method) for update in updates: yield from self._on_update(update) self._update_id = update.update_id + 1 # if not len(updates): # yield from asyncio.sleep(10) except KeyboardInterrupt: log.info('Stopping ...') for plugin in self._plugins: yield from plugin.stopPlugin() @asyncio.coroutine def _on_update(self, update): for plugin in self._plugins: handled = yield from plugin.on_message(update.message) if handled: break @asyncio.coroutine def _send_method(self, method): result = yield from self._client.send_method(method) return result
class TestAsyncioClient(TestCase): _client = None def setUp(self): if self._client is None: self._client = AsyncioClient(env.token, self._on_update, env.proxy) def _on_update(self, message): pass @aioloop def test_poll(self): m = getUpdates() m.timeout = 5 m.limit = 5 updates = yield from self._client.send_method(m) for update in updates: print(update) @aioloop def test_send(self): m = sendMessage() m.chat_id = env.uid m.text = "Hi there" resp = yield from self._client.send_method(m) print(resp) @aioloop def test_send_photo(self): m = sendPhoto() m.chat_id = env.uid m.caption = "What is this?" import os m.photo = os.path.join(os.path.split(__file__)[0], "test.jpg") resp = yield from self._client.send_method(m) print(resp)
def setUp(self): if self._client is None: self._client = AsyncioClient(env.token, env.proxy)
class TestAsyncioClient(TestCase): _client = None def setUp(self): if self._client is None: self._client = AsyncioClient(env.token, env.proxy) def _on_update(self, message): pass @aioloop def test_poll(self): m = getUpdates() m.timeout = 5 m.limit = 5 updates = yield from self._client.send_method(m) for update in updates: print(update) @aioloop def test_send(self): m = sendMessage() m.chat_id = env.uid m.text = "Hi there" resp = yield from self._client.send_method(m) print(resp) @aioloop def test_send_photo_by_filename(self): m = sendPhoto() m.chat_id = env.uid m.caption = "What is this?" import os filename = os.path.join(os.path.split(__file__)[0], "test.jpg") m.photo = filename resp = yield from self._client.send_method(m) print(resp) @aioloop def test_send_photo_by_filehandle(self): m = sendPhoto() m.chat_id = env.uid m.caption = "What is this?" import os filename = os.path.join(os.path.split(__file__)[0], "test.jpg") with open(filename, 'rb') as fh: m.photo = fh resp = yield from self._client.send_method(m) print(resp) @aioloop def test_send_photo_by_bytes(self): m = sendPhoto() m.chat_id = env.uid m.caption = "What is this?" import os filename = os.path.join(os.path.split(__file__)[0], "test.jpg") import io import shutil bytes = io.BytesIO() with open(filename, 'rb') as fh: shutil.copyfileobj(fh, bytes) bytes.seek(0) m.photo = bytes resp = yield from self._client.send_method(m) print(resp)