Skip to content
Merged
Show file tree
Hide file tree
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
37 changes: 17 additions & 20 deletions examples/conversationbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@
def start(bot, update):
reply_keyboard = [['Boy', 'Girl', 'Other']]

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

return GENDER


def gender(bot, update):
user = update.message.from_user
logger.info("Gender of %s: %s" % (user.first_name, update.message.text))
bot.sendMessage(update.message.chat_id,
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.')
update.message.reply_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.')

return PHOTO

Expand All @@ -59,17 +58,17 @@ def photo(bot, update):
photo_file = bot.getFile(update.message.photo[-1].file_id)
photo_file.download('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, '
'or send /skip if you don\'t want to.')
update.message.reply_text('Gorgeous! Now, send me your location please, '
'or send /skip if you don\'t want to.')

return LOCATION


def skip_photo(bot, update):
user = update.message.from_user
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 '
'location please, or send /skip.')
update.message.reply_text('I bet you look great! Now, send me your location please, '
'or send /skip.')

return LOCATION

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

return BIO


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

return BIO


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

return ConversationHandler.END


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

return ConversationHandler.END

Expand Down
2 changes: 1 addition & 1 deletion examples/echobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def echo(bot):

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


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions examples/inlinebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi!')
update.message.reply_text('Hi!')


def help(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!')
update.message.reply_text('Help!')


def escape_markdown(text):
Expand Down Expand Up @@ -68,7 +68,7 @@ def inlinequery(bot, update):
"_%s_" % escape_markdown(query),
parse_mode=ParseMode.MARKDOWN)))

bot.answerInlineQuery(update.inline_query.id, results=results)
update.inline_query.answer(results)


def error(bot, update, error):
Expand Down
4 changes: 2 additions & 2 deletions examples/inlinekeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def start(bot, update):

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):
Expand All @@ -32,7 +32,7 @@ def button(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):
Expand Down
16 changes: 8 additions & 8 deletions examples/timerbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
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):
Expand All @@ -46,33 +46,33 @@ def set(bot, update, args, job_queue):
# args[0] should contain the time for the timer in seconds
due = int(args[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

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

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

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"""
chat_id = update.message.chat_id

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

job = timers[chat_id]
job.schedule_removal()
del timers[chat_id]

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


def error(bot, update, error):
Expand All @@ -89,7 +89,7 @@ def main():
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", start))
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
dp.add_error_handler(error)
Expand Down