Skip to content

alegaballo/python-telegram-bot-builder

Repository files navigation

python-telegram-bot-builder

Create templates for Telegram bots from a config file

  1. Create a bot using @BotFather
  2. Set your bot parameters in config/conf.yml
  3. Run make_bot.py to create bot.py
  4. Run python bot.py to run your bot

Configuration

bot_token : [str] # token generated by @BotFather
private : [bool] # restrict the access to a list of users
allowed_users : [List] # list of user ids to grant access to in case private==true
inline : [bool] # enable/disable inline mode, if true it must be activated by @BotFather
handlers : [List] # list of handlers that will be generated
# one handler is specified by:
type : [str] # one of "msg","cmd", msg for text, cmd for commands (e.g. /start)
function : [str] # name of the function associated to this handler
content_type : [str] # expected content of the msg, one of "text", "photo", "audio", etc.. See https://core.telegram.org/bots/api#sendmessage
return_content_type : [str] # expected content of the return msg, same as content_type

For more information, take a look at: config/config.example.yml
The generated bot template is in: generated_bot.py

NOTE:

At the moment the bot token is in clear text in bot.py, this is a dangerous behavior if you want to version your bot. Once you have generated the token, export it in an environment variable and use that in bot.py.

Example:

.bashrc

export MY_BOT_TOKEN='S0m3HexString'

bot.py

updater = Updater(token=os.environ["MY_BOT_TOKEN"], use_context=True)

TODOs:

  • add arg parser
  • fix jinja template whitespaces management