コード例 #1
0
def main():
    global update_id
    # Telegram Bot Authorization Token
    bot = telegram.Bot(apikeys.getTelegramBotKey())

    # get the first pending update_id, this is so we can skip over it in case
    # we get an "Unauthorized" exception.
    try:
        update_id = bot.getUpdates()[0].update_id
    except IndexError:
        update_id = None

    logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    while True:
        try:
            echo(bot)
        except NetworkError:
            sleep(1)
        except Unauthorized:
            # The user has removed or blocked the bot.
            update_id += 1
コード例 #2
0
def botMessage(message):
    # Telegram Bot Authorization Token
    bot = telegram.Bot(apikeys.getTelegramBotKey())

    # get the first pending update_id, this is so we can skip over it in case
    # we get an "Unauthorized" exception.
    try:
        update_id = bot.getUpdates()[0].update_id
    except IndexError:
        update_id = None

    logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    try:
        update_id = echo(bot, update_id, message)
    except telegram.TelegramError as e:
        # These are network problems with Telegram.
        if e.message in ("Bad Gateway", "Timed out"):
            sleep(1)
        elif e.message == "Unauthorized":
            # The user has removed or blocked the bot.
            update_id += 1
        else:
            raise e