def downloadFile(self, file_id, dest): f = yield from self.getFile(file_id) # `file_path` is optional in File object if 'file_path' not in f: raise telepot.TelegramError('No file_path returned', None) try: r = yield from asyncio.wait_for( aiohttp.get(self._fileurl(f['file_path'])), self._http_timeout) d = dest if isinstance(dest, io.IOBase) else open(dest, 'wb') while 1: chunk = yield from r.content.read(self._file_chunk_size) if not chunk: break d.write(chunk) d.flush() finally: if not isinstance(dest, io.IOBase) and 'd' in locals(): d.close() if 'r' in locals(): r.close()
def __init__(self, hangupsbot): self.config = hangupsbot.config.get_by_path(['telesync']) if self.config['enabled']: try: super(TelegramBot, self).__init__(self.config['api_key']) except Exception as e: raise telepot.TelegramError("Couldn't initialize telesync", 10) if "bot_name" in hangupsbot.config.get_by_path(["telesync"]): self.name = hangupsbot.config.get_by_path(["telesync" ])["bot_name"] else: self.name = "bot" self.commands = {} self.onMessageCallback = TelegramBot.on_message self.onPhotoCallback = TelegramBot.on_photo self.onStickerCallback = TelegramBot.on_sticker self.onUserJoinCallback = TelegramBot.on_user_join self.onUserLeaveCallback = TelegramBot.on_user_leave self.onLocationShareCallback = TelegramBot.on_location_share self.onSupergroupUpgradeCallback = TelegramBot.on_supoergroup_upgrade self.ho_bot = hangupsbot else: logger.info('telesync disabled in config.json')
def _parse(self, response): try: data = yield from response.json() except ValueError: text = yield from response.text() raise telepot.BadHTTPResponse(response.status, text) if data['ok']: return data['result'] else: raise telepot.TelegramError(data['description'], data['error_code'])