Skip to content

Commit f8a17cd

Browse files
jsmnbomEldinnie
authored andcommitted
Revert "Context based callbacks (python-telegram-bot#1100)"
This reverts commit 247577b.
1 parent 9e2357b commit f8a17cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+618
-1390
lines changed

CHANGES.rst

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
=======
22
Changes
33
=======
4-
**2018-??-??**
5-
*Released 11.0.0*
6-
7-
Context based callbacks:
8-
See https://git.io/vp113 for help.
9-
10-
- Use of `pass_` in handlers is deprecated.
11-
- Instead use `use_context=True` on `Updater` or `Dispatcher` and change callback from (bot, update, others...) to (update, context).
12-
- This also applies to error handlers `Dispatcher.add_error_handler` and JobQueue jobs (change (bot, job) to (context) here).
13-
- For users with custom handlers subclassing Handler, this is mostly backwards compatible, but to use the new context based callbacks you need to implement the new collect_additional_context method.
14-
- Passing bot to JobQueue.__init__ is deprecated. Use JobQueue.set_dispatcher with a dispatcher instead.
15-
16-
Other:
17-
- Handlers should be faster due to deduped logic.
18-
19-
Other removals:
20-
- Remove the ability to use filter lists in handlers.
21-
- Remove the last CamelCase CheckUpdate methods from the handlers we missed earlier.
22-
23-
244
**2018-05-02**
255
*Released 10.1.0*
266

docs/source/telegram.ext.callbackcontext.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/source/telegram.ext.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ telegram.ext package
1010
telegram.ext.jobqueue
1111
telegram.ext.messagequeue
1212
telegram.ext.delayqueue
13-
telegram.ext.callbackcontext
1413

1514
Handlers
1615
--------

examples/conversationbot.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
bot.
1818
"""
1919

20-
import logging
21-
2220
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
2321
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
2422
ConversationHandler)
2523

24+
import logging
25+
2626
# Enable logging
2727
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
2828
level=logging.INFO)
@@ -32,7 +32,7 @@
3232
GENDER, PHOTO, LOCATION, BIO = range(4)
3333

3434

35-
def start(update, context):
35+
def start(bot, update):
3636
reply_keyboard = [['Boy', 'Girl', 'Other']]
3737

3838
update.message.reply_text(
@@ -44,7 +44,7 @@ def start(update, context):
4444
return GENDER
4545

4646

47-
def gender(update, context):
47+
def gender(bot, update):
4848
user = update.message.from_user
4949
logger.info("Gender of %s: %s", user.first_name, update.message.text)
5050
update.message.reply_text('I see! Please send me a photo of yourself, '
@@ -54,9 +54,9 @@ def gender(update, context):
5454
return PHOTO
5555

5656

57-
def photo(update, context):
57+
def photo(bot, update):
5858
user = update.message.from_user
59-
photo_file = update.message.photo[-1].get_file()
59+
photo_file = bot.get_file(update.message.photo[-1].file_id)
6060
photo_file.download('user_photo.jpg')
6161
logger.info("Photo of %s: %s", user.first_name, 'user_photo.jpg')
6262
update.message.reply_text('Gorgeous! Now, send me your location please, '
@@ -65,7 +65,7 @@ def photo(update, context):
6565
return LOCATION
6666

6767

68-
def skip_photo(update, context):
68+
def skip_photo(bot, update):
6969
user = update.message.from_user
7070
logger.info("User %s did not send a photo.", user.first_name)
7171
update.message.reply_text('I bet you look great! Now, send me your location please, '
@@ -74,7 +74,7 @@ def skip_photo(update, context):
7474
return LOCATION
7575

7676

77-
def location(update, context):
77+
def location(bot, update):
7878
user = update.message.from_user
7979
user_location = update.message.location
8080
logger.info("Location of %s: %f / %f", user.first_name, user_location.latitude,
@@ -85,7 +85,7 @@ def location(update, context):
8585
return BIO
8686

8787

88-
def skip_location(update, context):
88+
def skip_location(bot, update):
8989
user = update.message.from_user
9090
logger.info("User %s did not send a location.", user.first_name)
9191
update.message.reply_text('You seem a bit paranoid! '
@@ -94,15 +94,15 @@ def skip_location(update, context):
9494
return BIO
9595

9696

97-
def bio(update, context):
97+
def bio(bot, update):
9898
user = update.message.from_user
9999
logger.info("Bio of %s: %s", user.first_name, update.message.text)
100100
update.message.reply_text('Thank you! I hope we can talk again some day.')
101101

102102
return ConversationHandler.END
103103

104104

105-
def cancel(update, context):
105+
def cancel(bot, update):
106106
user = update.message.from_user
107107
logger.info("User %s canceled the conversation.", user.first_name)
108108
update.message.reply_text('Bye! I hope we can talk again some day.',
@@ -111,16 +111,14 @@ def cancel(update, context):
111111
return ConversationHandler.END
112112

113113

114-
def error(update, context):
114+
def error(bot, update, error):
115115
"""Log Errors caused by Updates."""
116-
logger.warning('Update "%s" caused error "%s"', update, context.error)
116+
logger.warning('Update "%s" caused error "%s"', update, error)
117117

118118

119119
def main():
120-
# Create the Updater and pass it your bot's token.
121-
# Make sure to set use_context=True to use the new context based callbacks
122-
# Post version 12 this will no longer be necessary
123-
updater = Updater("TOKEN", use_context=True)
120+
# Create the EventHandler and pass it your bot's token.
121+
updater = Updater("TOKEN")
124122

125123
# Get the dispatcher to register handlers
126124
dp = updater.dispatcher

examples/conversationbot2.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
bot.
1818
"""
1919

20-
import logging
21-
2220
from telegram import ReplyKeyboardMarkup
2321
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
2422
ConversationHandler)
2523

24+
import logging
25+
2626
# Enable logging
2727
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
2828
level=logging.INFO)
@@ -46,7 +46,7 @@ def facts_to_str(user_data):
4646
return "\n".join(facts).join(['\n', '\n'])
4747

4848

49-
def start(update, context):
49+
def start(bot, update):
5050
update.message.reply_text(
5151
"Hi! My name is Doctor Botter. I will hold a more complex conversation with you. "
5252
"Why don't you tell me something about yourself?",
@@ -55,24 +55,23 @@ def start(update, context):
5555
return CHOOSING
5656

5757

58-
def regular_choice(update, context):
58+
def regular_choice(bot, update, user_data):
5959
text = update.message.text
60-
context.user_data['choice'] = text
60+
user_data['choice'] = text
6161
update.message.reply_text(
6262
'Your {}? Yes, I would love to hear about that!'.format(text.lower()))
6363

6464
return TYPING_REPLY
6565

6666

67-
def custom_choice(update, context):
67+
def custom_choice(bot, update):
6868
update.message.reply_text('Alright, please send me the category first, '
6969
'for example "Most impressive skill"')
7070

7171
return TYPING_CHOICE
7272

7373

74-
def received_information(update, context):
75-
user_data = context.user_data
74+
def received_information(bot, update, user_data):
7675
text = update.message.text
7776
category = user_data['choice']
7877
user_data[category] = text
@@ -86,8 +85,7 @@ def received_information(update, context):
8685
return CHOOSING
8786

8887

89-
def done(update, context):
90-
user_data = context.user_data
88+
def done(bot, update, user_data):
9189
if 'choice' in user_data:
9290
del user_data['choice']
9391

@@ -99,16 +97,14 @@ def done(update, context):
9997
return ConversationHandler.END
10098

10199

102-
def error(update, context):
100+
def error(bot, update, error):
103101
"""Log Errors caused by Updates."""
104102
logger.warning('Update "%s" caused error "%s"', update, error)
105103

106104

107105
def main():
108106
# Create the Updater and pass it your bot's token.
109-
# Make sure to set use_context=True to use the new context based callbacks
110-
# Post version 12 this will no longer be necessary
111-
updater = Updater("TOKEN", use_context=True)
107+
updater = Updater("TOKEN")
112108

113109
# Get the dispatcher to register handlers
114110
dp = updater.dispatcher

examples/echobot2.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
bot.
1818
"""
1919

20-
import logging
21-
2220
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
21+
import logging
2322

2423
# Enable logging
2524
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
@@ -30,32 +29,30 @@
3029

3130
# Define a few command handlers. These usually take the two arguments bot and
3231
# update. Error handlers also receive the raised TelegramError object in error.
33-
def start(update, context):
32+
def start(bot, update):
3433
"""Send a message when the command /start is issued."""
3534
update.message.reply_text('Hi!')
3635

3736

38-
def help(update, context):
37+
def help(bot, update):
3938
"""Send a message when the command /help is issued."""
4039
update.message.reply_text('Help!')
4140

4241

43-
def echo(update, context):
42+
def echo(bot, update):
4443
"""Echo the user message."""
4544
update.message.reply_text(update.message.text)
4645

4746

48-
def error(update, context):
47+
def error(bot, update, error):
4948
"""Log Errors caused by Updates."""
50-
logger.warning('Update "%s" caused error "%s"', update, context.error)
49+
logger.warning('Update "%s" caused error "%s"', update, error)
5150

5251

5352
def main():
5453
"""Start the bot."""
55-
# Create the Updater and pass it your bot's token.
56-
# Make sure to set use_context=True to use the new context based callbacks
57-
# Post version 12 this will no longer be necessary
58-
updater = Updater("TOKEN", use_context=True)
54+
# Create the EventHandler and pass it your bot's token.
55+
updater = Updater("TOKEN")
5956

6057
# Get the dispatcher to register handlers
6158
dp = updater.dispatcher

examples/inlinebot.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
Press Ctrl-C on the command line or send a signal to the process to stop the
1717
bot.
1818
"""
19-
import logging
2019
from uuid import uuid4
2120

21+
from telegram.utils.helpers import escape_markdown
22+
2223
from telegram import InlineQueryResultArticle, ParseMode, \
2324
InputTextMessageContent
2425
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
25-
from telegram.utils.helpers import escape_markdown
26+
import logging
2627

2728
# Enable logging
2829
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
@@ -33,17 +34,17 @@
3334

3435
# Define a few command handlers. These usually take the two arguments bot and
3536
# update. Error handlers also receive the raised TelegramError object in error.
36-
def start(update, context):
37+
def start(bot, update):
3738
"""Send a message when the command /start is issued."""
3839
update.message.reply_text('Hi!')
3940

4041

41-
def help(update, context):
42+
def help(bot, update):
4243
"""Send a message when the command /help is issued."""
4344
update.message.reply_text('Help!')
4445

4546

46-
def inlinequery(update, context):
47+
def inlinequery(bot, update):
4748
"""Handle the inline query."""
4849
query = update.inline_query.query
4950
results = [
@@ -68,16 +69,14 @@ def inlinequery(update, context):
6869
update.inline_query.answer(results)
6970

7071

71-
def error(update, context):
72+
def error(bot, update, error):
7273
"""Log Errors caused by Updates."""
73-
logger.warning('Update "%s" caused error "%s"', update, context.error)
74+
logger.warning('Update "%s" caused error "%s"', update, error)
7475

7576

7677
def main():
7778
# Create the Updater and pass it your bot's token.
78-
# Make sure to set use_context=True to use the new context based callbacks
79-
# Post version 12 this will no longer be necessary
80-
updater = Updater("TOKEN", use_context=True)
79+
updater = Updater("TOKEN")
8180

8281
# Get the dispatcher to register handlers
8382
dp = updater.dispatcher

examples/inlinekeyboard.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# This program is dedicated to the public domain under the CC0 license.
66
"""
77
import logging
8-
98
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
109
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
1110

@@ -14,7 +13,7 @@
1413
logger = logging.getLogger(__name__)
1514

1615

17-
def start(update, context):
16+
def start(bot, update):
1817
keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
1918
InlineKeyboardButton("Option 2", callback_data='2')],
2019

@@ -25,26 +24,26 @@ def start(update, context):
2524
update.message.reply_text('Please choose:', reply_markup=reply_markup)
2625

2726

28-
def button(update, context):
27+
def button(bot, update):
2928
query = update.callback_query
3029

31-
query.edit_message_text(text="Selected option: {}".format(query.data))
30+
bot.edit_message_text(text="Selected option: {}".format(query.data),
31+
chat_id=query.message.chat_id,
32+
message_id=query.message.message_id)
3233

3334

34-
def help(update, context):
35+
def help(bot, update):
3536
update.message.reply_text("Use /start to test this bot.")
3637

3738

38-
def error(update, context):
39+
def error(bot, update, error):
3940
"""Log Errors caused by Updates."""
40-
logger.warning('Update "%s" caused error "%s"', update, context.error)
41+
logger.warning('Update "%s" caused error "%s"', update, error)
4142

4243

4344
def main():
4445
# Create the Updater and pass it your bot's token.
45-
# Make sure to set use_context=True to use the new context based callbacks
46-
# Post version 12 this will no longer be necessary
47-
updater = Updater("TOKEN", use_context=True)
46+
updater = Updater("TOKEN")
4847

4948
updater.dispatcher.add_handler(CommandHandler('start', start))
5049
updater.dispatcher.add_handler(CallbackQueryHandler(button))

0 commit comments

Comments
 (0)
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