Skip to content

Commit be675f0

Browse files
authored
update all examples to use instance methods (python-telegram-bot#421)
1 parent c49058d commit be675f0

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

examples/conversationbot.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@
3535
def start(bot, update):
3636
reply_keyboard = [['Boy', 'Girl', 'Other']]
3737

38-
bot.sendMessage(update.message.chat_id,
39-
text='Hi! My name is Professor Bot. I will hold a conversation with you. '
40-
'Send /cancel to stop talking to me.\n\n'
41-
'Are you a boy or a girl?',
42-
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
38+
update.message.reply_text(
39+
'Hi! My name is Professor Bot. I will hold a conversation with you. '
40+
'Send /cancel to stop talking to me.\n\n'
41+
'Are you a boy or a girl?',
42+
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
4343

4444
return GENDER
4545

4646

4747
def gender(bot, update):
4848
user = update.message.from_user
4949
logger.info("Gender of %s: %s" % (user.first_name, update.message.text))
50-
bot.sendMessage(update.message.chat_id,
51-
text='I see! Please send me a photo of yourself, '
52-
'so I know what you look like, or send /skip if you don\'t want to.')
50+
update.message.reply_text('I see! Please send me a photo of yourself, '
51+
'so I know what you look like, or send /skip if you don\'t want to.')
5352

5453
return PHOTO
5554

@@ -59,17 +58,17 @@ def photo(bot, update):
5958
photo_file = bot.getFile(update.message.photo[-1].file_id)
6059
photo_file.download('user_photo.jpg')
6160
logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg'))
62-
bot.sendMessage(update.message.chat_id, text='Gorgeous! Now, send me your location please, '
63-
'or send /skip if you don\'t want to.')
61+
update.message.reply_text('Gorgeous! Now, send me your location please, '
62+
'or send /skip if you don\'t want to.')
6463

6564
return LOCATION
6665

6766

6867
def skip_photo(bot, update):
6968
user = update.message.from_user
7069
logger.info("User %s did not send a photo." % user.first_name)
71-
bot.sendMessage(update.message.chat_id, text='I bet you look great! Now, send me your '
72-
'location please, or send /skip.')
70+
update.message.reply_text('I bet you look great! Now, send me your location please, '
71+
'or send /skip.')
7372

7473
return LOCATION
7574

@@ -79,35 +78,33 @@ def location(bot, update):
7978
user_location = update.message.location
8079
logger.info("Location of %s: %f / %f"
8180
% (user.first_name, user_location.latitude, user_location.longitude))
82-
bot.sendMessage(update.message.chat_id, text='Maybe I can visit you sometime! '
83-
'At last, tell me something about yourself.')
81+
update.message.reply_text('Maybe I can visit you sometime! '
82+
'At last, tell me something about yourself.')
8483

8584
return BIO
8685

8786

8887
def skip_location(bot, update):
8988
user = update.message.from_user
9089
logger.info("User %s did not send a location." % user.first_name)
91-
bot.sendMessage(update.message.chat_id, text='You seem a bit paranoid! '
92-
'At last, tell me something about yourself.')
90+
update.message.reply_text('You seem a bit paranoid! '
91+
'At last, tell me something about yourself.')
9392

9493
return BIO
9594

9695

9796
def bio(bot, update):
9897
user = update.message.from_user
9998
logger.info("Bio of %s: %s" % (user.first_name, update.message.text))
100-
bot.sendMessage(update.message.chat_id,
101-
text='Thank you! I hope we can talk again some day.')
99+
update.message.reply_text('Thank you! I hope we can talk again some day.')
102100

103101
return ConversationHandler.END
104102

105103

106104
def cancel(bot, update):
107105
user = update.message.from_user
108106
logger.info("User %s canceled the conversation." % user.first_name)
109-
bot.sendMessage(update.message.chat_id,
110-
text='Bye! I hope we can talk again some day.')
107+
update.message.reply_text('Bye! I hope we can talk again some day.')
111108

112109
return ConversationHandler.END
113110

examples/echobot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def echo(bot):
4646

4747
if update.message: # your bot can receive updates without messages
4848
# Reply to the message
49-
bot.sendMessage(chat_id=chat_id, text=update.message.text)
49+
update.message.reply_text(update.message.text)
5050

5151

5252
if __name__ == '__main__':

examples/inlinebot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
# Define a few command handlers. These usually take the two arguments bot and
3535
# update. Error handlers also receive the raised TelegramError object in error.
3636
def start(bot, update):
37-
bot.sendMessage(update.message.chat_id, text='Hi!')
37+
update.message.reply_text('Hi!')
3838

3939

4040
def help(bot, update):
41-
bot.sendMessage(update.message.chat_id, text='Help!')
41+
update.message.reply_text('Help!')
4242

4343

4444
def escape_markdown(text):
@@ -68,7 +68,7 @@ def inlinequery(bot, update):
6868
"_%s_" % escape_markdown(query),
6969
parse_mode=ParseMode.MARKDOWN)))
7070

71-
bot.answerInlineQuery(update.inline_query.id, results=results)
71+
update.inline_query.answer(results)
7272

7373

7474
def error(bot, update, error):

examples/inlinekeyboard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def start(bot, update):
2020

2121
reply_markup = InlineKeyboardMarkup(keyboard)
2222

23-
bot.sendMessage(update.message.chat_id, text="Please choose:", reply_markup=reply_markup)
23+
update.message.reply_text('Please choose:', reply_markup=reply_markup)
2424

2525

2626
def button(bot, update):
@@ -32,7 +32,7 @@ def button(bot, update):
3232

3333

3434
def help(bot, update):
35-
bot.sendMessage(update.message.chat_id, text="Use /start to test this bot.")
35+
update.message.reply_text("Use /start to test this bot.")
3636

3737

3838
def error(bot, update, error):

examples/timerbot.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# Define a few command handlers. These usually take the two arguments bot and
3232
# update. Error handlers also receive the raised TelegramError object in error.
3333
def start(bot, update):
34-
bot.sendMessage(update.message.chat_id, text='Hi! Use /set <seconds> to ' 'set a timer')
34+
update.message.reply_text('Hi! Use /set <seconds> to set a timer')
3535

3636

3737
def alarm(bot, job):
@@ -46,33 +46,33 @@ def set(bot, update, args, job_queue):
4646
# args[0] should contain the time for the timer in seconds
4747
due = int(args[0])
4848
if due < 0:
49-
bot.sendMessage(chat_id, text='Sorry we can not go back to future!')
49+
update.message.reply_text('Sorry we can not go back to future!')
5050
return
5151

5252
# Add job to queue
5353
job = Job(alarm, due, repeat=False, context=chat_id)
5454
timers[chat_id] = job
5555
job_queue.put(job)
5656

57-
bot.sendMessage(chat_id, text='Timer successfully set!')
57+
update.message.reply_text('Timer successfully set!')
5858

5959
except (IndexError, ValueError):
60-
bot.sendMessage(chat_id, text='Usage: /set <seconds>')
60+
update.message.reply_text('Usage: /set <seconds>')
6161

6262

63-
def unset(bot, update, job_queue):
63+
def unset(bot, update):
6464
"""Removes the job if the user changed their mind"""
6565
chat_id = update.message.chat_id
6666

6767
if chat_id not in timers:
68-
bot.sendMessage(chat_id, text='You have no active timer')
68+
update.message.reply_text('You have no active timer')
6969
return
7070

7171
job = timers[chat_id]
7272
job.schedule_removal()
7373
del timers[chat_id]
7474

75-
bot.sendMessage(chat_id, text='Timer successfully unset!')
75+
update.message.reply_text('Timer successfully unset!')
7676

7777

7878
def error(bot, update, error):
@@ -89,7 +89,7 @@ def main():
8989
dp.add_handler(CommandHandler("start", start))
9090
dp.add_handler(CommandHandler("help", start))
9191
dp.add_handler(CommandHandler("set", set, pass_args=True, pass_job_queue=True))
92-
dp.add_handler(CommandHandler("unset", unset, pass_job_queue=True))
92+
dp.add_handler(CommandHandler("unset", unset))
9393

9494
# log all errors
9595
dp.add_error_handler(error)

0 commit comments

Comments
 (0)