Update Examples with the str.format() syntax.#870
Update Examples with the str.format() syntax.#870tsnoam merged 12 commits intopython-telegram-bot:masterfrom SitiSchu:sitischu-patch-1
Conversation
conversationbot.py:
Use `str.format()`
conversationbot2.py:
Use `str.format()`
make the states dict flake8 compatible.
echobot.py:
Change Docstring to a Docstring using triple quotes.
Add Docstrings to `main()` and `echo()`
echobot2.py:
Add File Description to Docstring
Add License notice to Docstring
Add Docstrings to `start()`, `help()`, `echo()` and `main()`
inlinebot.py:
Use `str.format()`
Add File Description to Docstring
Add License notice to Docstring
Add Docstring to `start()`, `help()`, `inlinequery()`, `error()`
Create the list "results" with the Inline Results instead of appending them.
inlinekeyboard.py:
Use `str.format()`
Put the Update in a `main()` function.
paymentbot.py:
Use `str.format()`
Shorten comment on how to get a provider token to avoid the comment being longer than 99 characters
timerbot.py:
Use `str.format()`
Add Docstrings to `alarm()`, `set_timer()`, `unset()`
Rename `set()` to `set_timer()` to avoud shadowing the built-in `set()`
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.79% +0.35%
==========================================
Files 101 101
Lines 3985 3985
Branches 603 603
==========================================
+ Hits 3644 3658 +14
+ Misses 195 189 -6
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
Thanks to @KamranMackey for making me aware of the fact that Log messages shouldnt use str.format() according to this article: http://reinout.vanrees.org/weblog/2015/06/05/logging-formatting.html
examples/conversationbot.py
Outdated
| def gender(bot, update): | ||
| user = update.message.from_user | ||
| logger.info("Gender of %s: %s" % (user.first_name, update.message.text)) | ||
| logger.info("Gender of {}: {}".format(user.first_name, update.message.text)) |
There was a problem hiding this comment.
Good catch here, we shouldn't be formatting logs ourselves but rather let logging do it. Unfortunately logging only work with %s notation. So please change to:
logger.info("Gender of %s: %s", user.first_name, update.message.text)
edit: I see that part of the logging occurrences you've done that way.
examples/conversationbot.py
Outdated
| photo_file = bot.get_file(update.message.photo[-1].file_id) | ||
| photo_file.download('user_photo.jpg') | ||
| logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg')) | ||
| logger.info("Photo of {}: {}".format(user.first_name, 'user_photo.jpg')) |
examples/conversationbot.py
Outdated
| def skip_photo(bot, update): | ||
| user = update.message.from_user | ||
| logger.info("User %s did not send a photo." % user.first_name) | ||
| logger.info("User {} did not send a photo.".format(user.first_name)) |
examples/conversationbot.py
Outdated
| logger.info("Location of %s: %f / %f" | ||
| % (user.first_name, user_location.latitude, user_location.longitude)) | ||
| logger.info("Location of {}: {} / {}".format( | ||
| user.first_name, user_location.latitude, user_location.longitude)) |
examples/conversationbot2.py
Outdated
| reply_markup=markup) | ||
| "Hi! My name is Doctor Botter. I will hold a more complex conversation with you. " | ||
| "Why don't you tell me something about yourself?", | ||
| reply_markup=markup) |
There was a problem hiding this comment.
why did you change the indentation? please revert.
examples/conversationbot.py
Outdated
| def cancel(bot, update): | ||
| user = update.message.from_user | ||
| logger.info("User %s canceled the conversation." % user.first_name) | ||
| logger.info("User {} canceled the conversation.".format(user.first_name)) |
examples/conversationbot2.py
Outdated
| }, | ||
|
|
||
| fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)] | ||
| ) |
There was a problem hiding this comment.
why did you change the indentation here? please revert
examples/inlinebot.py
Outdated
| title="Italic", | ||
| input_message_content=InputTextMessageContent( | ||
| "_{}_".format(escape_markdown(query)), | ||
| parse_mode=ParseMode.MARKDOWN))] |
There was a problem hiding this comment.
why did you change the indentation here? please revert.
examples/paymentbot.py
Outdated
| # select a payload just for you to recognize its the donation from your bot | ||
| payload = "Custom-Payload" | ||
| # get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | ||
| # to get your provider_token see https://core.telegram.org/bots/payments#getting-a-token |
There was a problem hiding this comment.
If we're into grammar, "In order to get a provider_token ..." would be more appropriate.
examples/paymentbot.py
Outdated
| # select a payload just for you to recognize its the donation from your bot | ||
| payload = "Custom-Payload" | ||
| # get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | ||
| # check https://core.telegram.org/bots/payments#supported-currencies for more details |
Removed the Extra Indent in the start() function.
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.79% +0.35%
==========================================
Files 101 101
Lines 3985 3985
Branches 603 603
==========================================
+ Hits 3644 3658 +14
+ Misses 195 189 -6
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.73% +0.29%
==========================================
Files 101 101
Lines 3985 4055 +70
Branches 603 620 +17
==========================================
+ Hits 3644 3720 +76
- Misses 195 197 +2
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
tsnoam
left a comment
There was a problem hiding this comment.
Thanks for all the changes. Unfortunately, I'm afraid I'll have to ask you a few more changes, mostly regarding indentation.
examples/conversationbot2.py
Outdated
| reply_markup=markup) | ||
| "{}" | ||
| "You can tell me more, or change your opinion on something.".format( | ||
| facts_to_str(user_data)), reply_markup=markup) |
There was a problem hiding this comment.
Indentation here still needs fixing.
Try using autopep8 on the file.
examples/conversationbot2.py
Outdated
| pass_user_data=True), | ||
| ], | ||
| }, | ||
| }, |
There was a problem hiding this comment.
autopep8 will also fix indentation here.
examples/conversationbot2.py
Outdated
|
|
||
| fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)] | ||
| ) | ||
| ) |
There was a problem hiding this comment.
autopep8 will also fix indentation here.
examples/inlinebot.py
Outdated
| title="Italic", | ||
| input_message_content=InputTextMessageContent( | ||
| "_{}_".format(escape_markdown(query)), | ||
| parse_mode=ParseMode.MARKDOWN))] |
There was a problem hiding this comment.
autopep8 can be of assistant here as well.
examples/paymentbot.py
Outdated
| # select a payload just for you to recognize its the donation from your bot | ||
| payload = "Custom-Payload" | ||
| # get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | ||
| # In order to get a provider_token to get your provider_token see |
There was a problem hiding this comment.
In order to get a provider_token to get your provider_token see https://core.telegram.org/bots/payments#getting-a-token
Use autopep8 on the conversationbot2.py Fix the provide token comment (duplicated some words)
|
Did all changes except the one to inlinebot.py:inlinequery() since I'm not sure if autopep8 makes it better. |
|
I formated the InlineQuery results in inlinebot.py like @jh0ker suggested with a linebreak before the |
jh0ker
left a comment
There was a problem hiding this comment.
replace logger.warn with logger.warning as per https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/conversationbot.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/conversationbot2.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/echobot2.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/inlinebot.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warning('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this back to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/inlinekeyboard.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logging.warning('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this back to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/paymentbot.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/timerbot.py
Outdated
|
|
||
| def error(bot, update, error): | ||
| logger.warning('Update "%s" caused error "%s"' % (update, error)) | ||
| logger.warn('Update "%s" caused error "%s"', update, error) |
There was a problem hiding this comment.
Can you change this back to logger.warning? Logger.warn is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
|
@SitiSchu A bit more nitpicking :P |
|
@jh0ker PyCharm told me about this but I wasn't sure if Py2 supports it. I checked and it does support it so I'll change it today ^^. |
|
@SitiSchu Awesome, thanks :) |
|
I updated all error functions to the new |
examples/inlinebot.py
Outdated
| @@ -44,7 +45,7 @@ def help(bot, update): | |||
|
|
|||
|
|
|||
| def escape_markdown(text): | |||
There was a problem hiding this comment.
Can we replace this as well? We have telegram.utils.helpers.escape_markdown now.
|
@jh0ker Done. |
|
Any updates on this PR? :) |
|
LGTM @tsnoam any more changes requested? |
|
@SitiSchu Thankyou for your contribution. |


conversationbot.py:
Use
str.format()conversationbot2.py:
Use
str.format()make the states dict flake8 compatible.
echobot.py:
Change Docstring to a Docstring using triple quotes.
Add Docstrings to
main()andecho()echobot2.py:
Add File Description to Docstring
Add License notice to Docstring
Add Docstrings to
start(),help(),echo()andmain()inlinebot.py:
Use
str.format()Add File Description to Docstring
Add License notice to Docstring
Add Docstring to
start(),help(),inlinequery(),error()Create the list "results" with the Inline Results instead of appending them.
inlinekeyboard.py:
Use
str.format()Put the Update in a
main()function.paymentbot.py:
Use
str.format()Shorten comment on how to get a provider token to avoid the comment being longer than 99 characters
timerbot.py:
Use
str.format()Add Docstrings to
alarm(),set_timer(),unset()Rename
set()toset_timer()to avoud shadowing the built-inset()