Dev Ops in 30 Days

Python Telegram Bot

Today, I would like to share you a python wrapper that can enable you to build a telegram Chatbot in only one day, it is called python-telegram-bot. It has high community support and extremely easy to use. A lot of examples are provided and thus you could reference to the examples to achieve what you want.

----------

One of the quickstart example can be found at https://github.com/Eldinnie/ptb-heroku-skeleton/blob/master/herokubot.py. The codes are copied and shown at below:

import os, logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def start(bot, update):
update.effective_message.reply_text("Hi!")
def echo(bot, update):
update.effective_message.reply_text(update.effective_message.text)
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)
if __name__ == "__main__":
# Set these variable to the appropriate values
TOKEN = "Your token from @Botfather"
NAME = "The name of your app on Heroku"
# Port is given by Heroku
PORT = os.environ.get('PORT')
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Set up the Updater
updater = Updater(TOKEN)
dp = updater.dispatcher
# Add handlers
dp.add_handler(CommandHandler('start', start))
dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_error_handler(error)
# Start the webhook
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(NAME, TOKEN))
updater.idle()

----------

I have recently created a Chatbot (TDiaryBot) hosted in heroku, based on the telegram Chatbot framework. It is open sourced at github. If you are interested, you could take a look. ^^

You can try it out at telegram too.

That's all for today.

Thank you.

If you like what I am doing, you could buy me a coffee. ^^

Buy Me A Coffee

Pharaoh
7/11/2018
1:25p.m.

Comments