Skip to content

improve echobot with cleaner code and basic exception handling #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions examples/echobot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Simple Bot to reply Telegram messages
# Simple Bot to reply to Telegram messages
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -19,47 +20,52 @@

import logging
import telegram
from time import sleep


LAST_UPDATE_ID = None
try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError # python 2


def main():
global LAST_UPDATE_ID
update_id = None

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

# Telegram Bot Authorization Token
bot = telegram.Bot('TOKEN')

# This will be our global variable to keep the latest update_id when requesting
# for updates. It starts with the latest update_id if available.
try:
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
except IndexError:
LAST_UPDATE_ID = None

while True:
echo(bot)


def echo(bot):
global LAST_UPDATE_ID

# Request updates after the last updated_id
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
# chat_id is required to reply any message
try:
update_id = echo(bot, update_id)
except telegram.TelegramError as e:
# These are network problems with Telegram.
if e.message in ("Bad Gateway", "Timed out"):
sleep(1)
else:
raise e
except URLError as e:
# These are network problems on our end.
sleep(1)


def echo(bot, update_id):

# Request updates after the last update_id
for update in bot.getUpdates(offset=update_id, timeout=10):
# chat_id is required to reply to any message
chat_id = update.message.chat_id
reply_text = update.message.text
update_id = update.update_id + 1
message = update.message.text

if reply_text:
# Reply the message
if message:
# Reply to the message
bot.sendMessage(chat_id=chat_id,
text=reply_text)
text=message)

# Updates global offset to get the new updates
LAST_UPDATE_ID = update.update_id + 1
return update_id


if __name__ == '__main__':
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy