def _handle_new_poll_response(self, text): try: lines = text.strip().split("\n") title = lines[0] choices = lines[1:] except Exception: # Wrong format raise _ResponseException(self.RESPONSE_ERROR_NEWPOLL_FORMAT) if not choices: # No poll choices! raise _ResponseException(self.RESPONSE_ERROR_MISSING_CHOICES) try: with model.open_session(self._Session) as s: if self._has_active_polls(s): raise _ResponseException(self.RESPONSE_ERROR_POLL_EXIST) self._persist_new_poll(s, title, choices) self._bot.sendMessage(self._glance["chat_id"], self.RESPONSE_NEWPOLL_PERSISTED_F % title, parse_mode="Markdown") except Exception: Log.i(f"Failed persisting new poll \"{title}\": {choices}") raise
def _callback(): try: self._ensure_allowed_users() return self._do_handle() except _WhitelistException as e: Log.i("Disallowed user (%s) is chating with us" % self._msg["from"]["first_name"]) return self.RESPONSE_DISALLOWED_USER except Exception as e: Log.e("Failed while _do_handle", e) return self.RESPONSE_EXCEPTION
def _start(self): Log.i("Starting app") def _listener(msg): self._on_message(msg) def _callback_query_listener(msg): self._on_callback_query(msg) self._bot.setWebhook("") self._bot.message_loop({ "chat": _listener, "callback_query": _callback_query_listener, })
def _start(self): Log.i("Starting app") def _listener(msg): self._on_message(msg) def _inline_listener(msg): self._on_inline_message(msg) self._bot.setWebhook("") self._bot.message_loop({ "chat": _listener, "inline_query": _inline_listener, })
def handle(self): try: self._ensure_supported_chat() self._ensure_allowed_users() self._do_handle() except _ChanelException as e: pass except _WhitelistException as e: Log.i("Disallowed user (%s) is chating with us" % self._msg["from"]["first_name"]) self._bot.sendMessage(self._glance["chat_id"], self.RESPONSE_MD_DISALLOWED_USER, parse_mode = "Markdown") except Exception as e: Log.e("Failed while _do_handle", e) self._bot.sendMessage(self._glance["chat_id"], self.RESPONSE_EXCEPTION)
def handle(self): try: self._ensure_allowed_users() response = self._do_handle() except _WhitelistException as e: Log.i("Disallowed user (%s) is chating with us" % self._msg["from"]["first_name"]) response = self.RESPONSE_DISALLOWED_USER except Exception as e: Log.e("Failed while _do_handle", e) response = self.RESPONSE_EXCEPTION if isinstance(response, list): self._bot.answerInlineQuery(self._glance["query_id"], response) elif isinstance(response, tuple): self._bot.answerInlineQuery(self._glance["query_id"], *response) elif isinstance(response, dict): self._bot.answerInlineQuery(self._glance["query_id"], **response) else: raise ValueError("Invalid response format")
def run(self): import time self._start() Log.i("Running...") while True: time.sleep(10)
def __init__(self): Log.i("Initializing standalone app") self._bot = telepot.Bot(self.TELEGRAM_TOKEN)
def __init__(self): Log.i("Initializing PAW app") self._init_paw_telepot() self._bot = telepot.Bot(self.TELEGRAM_TOKEN)
def __init__(self): Log.i("Initializing standalone app") self._bot = telepot.Bot(self.TELEGRAM_TOKEN) self._answerer = telepot.helper.Answerer(self._bot)