Skip to content

Instantly share code, notes, and snippets.

Last active August 6, 2023 18:31
Show Gist options
  • Save aahnik/6c9dd519c61e718e4da5f0645aa11ada to your computer and use it in GitHub Desktop.
Save aahnik/6c9dd519c61e718e4da5f0645aa11ada to your computer and use it in GitHub Desktop.
A simple conversation bot that will ask user's name, and save it. Persistent, Polling, Shows Button, Has conversation flow, Conversation Handler, python-telegram-bot, ptb,

How to run

  1. Install dependancies.
pip3 install python-dotenv python-telegram-bot
  1. Then put the files of this gist inside a folder.

  2. Create a file .env which contains this.

# put your actual bot token
# keep this file secret
  1. Now run python3, you may press CTRL + c to stop the bot.

Notes for Deploying to Heroku

  • No need of the .env file, instead, set the value BOT_TOKEN in your config vars
  • change the code to use other form of persistence, because heroku will delete files that are created in run time, after your scale down or redeploy.

More info about persistence.

For furthur assistance : telegram-chat

import os
from dotenv import load_dotenv
BOT_TOKEN = os.getenv('BOT_TOKEN')
if __name__ == "__main__":
''' A simple conversation bot, that takes user's name and saves it.
- Uses pickle persistence for storing data. You can easily use other ways.
- Uses polling for fetching updates, you can easily use webhooks.
/start - replies if alive
/set_name - start a conversation to save name
/get_name - replies with user's name, if saved, else tells user to /set_name
import logging
from telegram.ext.filters import Filters
from telegram.ext.messagehandler import MessageHandler
from settings import BOT_TOKEN
from telegram import Update
from telegram.ext import (Updater,
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply
def start(update: Update, context: CallbackContext):
''' Replies to start command '''
update.message.reply_text('Hi! I am alive')
def set_name_handler(update: Update, context: CallbackContext):
''' Entry point of conversation this gives buttons to user'''
button = [[InlineKeyboardButton("name", callback_data='name')]]
markup = InlineKeyboardMarkup(button)
# you can add more buttons here
# learn more about inline keyboard
update.message.reply_text('Name button', reply_markup=markup)
def button_click_handler(update: Update, context: CallbackContext):
''' This gets executed on button click '''
query = update.callback_query
# shows a small notification inside chat
query.answer(f'button click {} recieved')
if == 'name':
query.edit_message_text(f'You clicked on "name"')
# asks for name, and prompts user to reply to it,
text='Send your name', reply_markup=ForceReply())
# learn more about forced reply
def name_input_by_user(update: Update, context: CallbackContext):
''' The user's reply to the name prompt comes here '''
name = update.message.text
# saves the name
context.user_data['name'] = name
update.message.reply_text(f'Your name is saved as {name[:100]}')
# ends this particular conversation flow
return ConversationHandler.END
def cancel(update: Update, context: CallbackContext):
'Name Conversation cancelled by user. Bye. Send /set_name to start again')
return ConversationHandler.END
def get_name(update: Update, context: CallbackContext):
''' Handle the get_name command. Replies the name of user if found. '''
value = context.user_data.get(
'name', 'Not found. Set your name using /set_name command')
if __name__ == "__main__":
# learn more about persistence
# pickle persistence
# example
# if you are deploying on cloud, connecting to a database may help
# because platforms like heroku deletes all extra files
pp = PicklePersistence(filename='mybot')
updater = Updater(token=BOT_TOKEN, persistence=pp)
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
_handlers = {}
_handlers['start_handler'] = CommandHandler('start', start)
# learn more about conversation handler
# official docs
# official example
_handlers['name_conversation_handler'] = ConversationHandler(
entry_points=[CommandHandler('set_name', set_name_handler)],
EXPECT_NAME: [MessageHandler(Filters.text, name_input_by_user)],
EXPECT_BUTTON_CLICK: [CallbackQueryHandler(button_click_handler)]
fallbacks=[CommandHandler('cancel', cancel)]
_handlers['get_name'] = CommandHandler('get_name', get_name)
for name, _handler in _handlers.items():
print(f'Adding handler {name}')
Copy link

Great example, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment