update all examples to use instance methods (#421)

This commit is contained in:
Jannes Höke 2016-09-24 15:32:22 +02:00 committed by GitHub
parent c49058dbb4
commit be675f0118
5 changed files with 31 additions and 34 deletions

View file

@ -35,11 +35,11 @@ GENDER, PHOTO, LOCATION, BIO = range(4)
def start(bot, update): def start(bot, update):
reply_keyboard = [['Boy', 'Girl', 'Other']] reply_keyboard = [['Boy', 'Girl', 'Other']]
bot.sendMessage(update.message.chat_id, update.message.reply_text(
text='Hi! My name is Professor Bot. I will hold a conversation with you. ' 'Hi! My name is Professor Bot. I will hold a conversation with you. '
'Send /cancel to stop talking to me.\n\n' 'Send /cancel to stop talking to me.\n\n'
'Are you a boy or a girl?', 'Are you a boy or a girl?',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)) reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
return GENDER return GENDER
@ -47,9 +47,8 @@ def start(bot, update):
def gender(bot, update): def gender(bot, update):
user = update.message.from_user user = update.message.from_user
logger.info("Gender of %s: %s" % (user.first_name, update.message.text)) logger.info("Gender of %s: %s" % (user.first_name, update.message.text))
bot.sendMessage(update.message.chat_id, update.message.reply_text('I see! Please send me a photo of yourself, '
text='I see! Please send me a photo of yourself, ' 'so I know what you look like, or send /skip if you don\'t want to.')
'so I know what you look like, or send /skip if you don\'t want to.')
return PHOTO return PHOTO
@ -59,8 +58,8 @@ def photo(bot, update):
photo_file = bot.getFile(update.message.photo[-1].file_id) photo_file = bot.getFile(update.message.photo[-1].file_id)
photo_file.download('user_photo.jpg') photo_file.download('user_photo.jpg')
logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg')) logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg'))
bot.sendMessage(update.message.chat_id, text='Gorgeous! Now, send me your location please, ' update.message.reply_text('Gorgeous! Now, send me your location please, '
'or send /skip if you don\'t want to.') 'or send /skip if you don\'t want to.')
return LOCATION return LOCATION
@ -68,8 +67,8 @@ def photo(bot, update):
def skip_photo(bot, update): def skip_photo(bot, update):
user = update.message.from_user user = update.message.from_user
logger.info("User %s did not send a photo." % user.first_name) logger.info("User %s did not send a photo." % user.first_name)
bot.sendMessage(update.message.chat_id, text='I bet you look great! Now, send me your ' update.message.reply_text('I bet you look great! Now, send me your location please, '
'location please, or send /skip.') 'or send /skip.')
return LOCATION return LOCATION
@ -79,8 +78,8 @@ def location(bot, update):
user_location = update.message.location user_location = update.message.location
logger.info("Location of %s: %f / %f" logger.info("Location of %s: %f / %f"
% (user.first_name, user_location.latitude, user_location.longitude)) % (user.first_name, user_location.latitude, user_location.longitude))
bot.sendMessage(update.message.chat_id, text='Maybe I can visit you sometime! ' update.message.reply_text('Maybe I can visit you sometime! '
'At last, tell me something about yourself.') 'At last, tell me something about yourself.')
return BIO return BIO
@ -88,8 +87,8 @@ def location(bot, update):
def skip_location(bot, update): def skip_location(bot, update):
user = update.message.from_user user = update.message.from_user
logger.info("User %s did not send a location." % user.first_name) logger.info("User %s did not send a location." % user.first_name)
bot.sendMessage(update.message.chat_id, text='You seem a bit paranoid! ' update.message.reply_text('You seem a bit paranoid! '
'At last, tell me something about yourself.') 'At last, tell me something about yourself.')
return BIO return BIO
@ -97,8 +96,7 @@ def skip_location(bot, update):
def bio(bot, update): def bio(bot, update):
user = update.message.from_user user = update.message.from_user
logger.info("Bio of %s: %s" % (user.first_name, update.message.text)) logger.info("Bio of %s: %s" % (user.first_name, update.message.text))
bot.sendMessage(update.message.chat_id, update.message.reply_text('Thank you! I hope we can talk again some day.')
text='Thank you! I hope we can talk again some day.')
return ConversationHandler.END return ConversationHandler.END
@ -106,8 +104,7 @@ def bio(bot, update):
def cancel(bot, update): def cancel(bot, update):
user = update.message.from_user user = update.message.from_user
logger.info("User %s canceled the conversation." % user.first_name) logger.info("User %s canceled the conversation." % user.first_name)
bot.sendMessage(update.message.chat_id, update.message.reply_text('Bye! I hope we can talk again some day.')
text='Bye! I hope we can talk again some day.')
return ConversationHandler.END return ConversationHandler.END

View file

@ -46,7 +46,7 @@ def echo(bot):
if update.message: # your bot can receive updates without messages if update.message: # your bot can receive updates without messages
# Reply to the message # Reply to the message
bot.sendMessage(chat_id=chat_id, text=update.message.text) update.message.reply_text(update.message.text)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -34,11 +34,11 @@ logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments bot and # Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error. # update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update): def start(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi!') update.message.reply_text('Hi!')
def help(bot, update): def help(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!') update.message.reply_text('Help!')
def escape_markdown(text): def escape_markdown(text):
@ -68,7 +68,7 @@ def inlinequery(bot, update):
"_%s_" % escape_markdown(query), "_%s_" % escape_markdown(query),
parse_mode=ParseMode.MARKDOWN))) parse_mode=ParseMode.MARKDOWN)))
bot.answerInlineQuery(update.inline_query.id, results=results) update.inline_query.answer(results)
def error(bot, update, error): def error(bot, update, error):

View file

@ -20,7 +20,7 @@ def start(bot, update):
reply_markup = InlineKeyboardMarkup(keyboard) reply_markup = InlineKeyboardMarkup(keyboard)
bot.sendMessage(update.message.chat_id, text="Please choose:", reply_markup=reply_markup) update.message.reply_text('Please choose:', reply_markup=reply_markup)
def button(bot, update): def button(bot, update):
@ -32,7 +32,7 @@ def button(bot, update):
def help(bot, update): def help(bot, update):
bot.sendMessage(update.message.chat_id, text="Use /start to test this bot.") update.message.reply_text("Use /start to test this bot.")
def error(bot, update, error): def error(bot, update, error):

View file

@ -31,7 +31,7 @@ timers = dict()
# Define a few command handlers. These usually take the two arguments bot and # Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error. # update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update): def start(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi! Use /set <seconds> to ' 'set a timer') update.message.reply_text('Hi! Use /set <seconds> to set a timer')
def alarm(bot, job): def alarm(bot, job):
@ -46,7 +46,7 @@ def set(bot, update, args, job_queue):
# args[0] should contain the time for the timer in seconds # args[0] should contain the time for the timer in seconds
due = int(args[0]) due = int(args[0])
if due < 0: if due < 0:
bot.sendMessage(chat_id, text='Sorry we can not go back to future!') update.message.reply_text('Sorry we can not go back to future!')
return return
# Add job to queue # Add job to queue
@ -54,25 +54,25 @@ def set(bot, update, args, job_queue):
timers[chat_id] = job timers[chat_id] = job
job_queue.put(job) job_queue.put(job)
bot.sendMessage(chat_id, text='Timer successfully set!') update.message.reply_text('Timer successfully set!')
except (IndexError, ValueError): except (IndexError, ValueError):
bot.sendMessage(chat_id, text='Usage: /set <seconds>') update.message.reply_text('Usage: /set <seconds>')
def unset(bot, update, job_queue): def unset(bot, update):
"""Removes the job if the user changed their mind""" """Removes the job if the user changed their mind"""
chat_id = update.message.chat_id chat_id = update.message.chat_id
if chat_id not in timers: if chat_id not in timers:
bot.sendMessage(chat_id, text='You have no active timer') update.message.reply_text('You have no active timer')
return return
job = timers[chat_id] job = timers[chat_id]
job.schedule_removal() job.schedule_removal()
del timers[chat_id] del timers[chat_id]
bot.sendMessage(chat_id, text='Timer successfully unset!') update.message.reply_text('Timer successfully unset!')
def error(bot, update, error): def error(bot, update, error):
@ -89,7 +89,7 @@ def main():
dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", start)) dp.add_handler(CommandHandler("help", start))
dp.add_handler(CommandHandler("set", set, pass_args=True, pass_job_queue=True)) dp.add_handler(CommandHandler("set", set, pass_args=True, pass_job_queue=True))
dp.add_handler(CommandHandler("unset", unset, pass_job_queue=True)) dp.add_handler(CommandHandler("unset", unset))
# log all errors # log all errors
dp.add_error_handler(error) dp.add_error_handler(error)