Create Ur Telegram bot
We find some times the media will be deleted, but it’s not a critical problem. I wanna monitor the condition and try use telegram bot to check the status. It’s very simple to use and you also can use it to integrate your works or check system status by Telegram.
Require API Token
You need to sign up a telegram account and create a bot with @botfather as the reference link.
You need to add @botfather as ur friend and create a new bot by “/newbot” message. if you need to know all commands, you can use “/” or “/help” to show all commands.
Botfather will ask u to provide a name for your bot, you can name your bot and get the token.
Create Ur Bot by Python
you can get telegram api from https://github.com/python-telegram-bot/python-telegram-bot, install by the command as below.
$ pip install python-telegram-bot --upgrade
There are 2 methods to create connection, webhook or polling updates. I don’t have personal server and domain, so I use polling updates.
The major bot command is “/status“, it will help me to call a function, MediaCount, which will get the media count from mongo. All I need is to keep the service and check the telegram.
# -*- coding: utf-8 -*- # @Time : 2017/4/25 上午11:24 # @Author : Yuhsuan # @File : Mongo_monitor.py # @Software: PyCharm Community Edition from pymongo import * from telegram.ext import Updater, CommandHandler, MessageHandler, Filters import logging import datetime
# Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
#update to mongodb def MediaCount(): res = 0 client = MongoClient("127.0.0.1", 27017) client.deepblu.authenticate('username', 'accesstoken', mechanism='MONGODB-CR') db = client.deepblu
if db.Media.find({}).count(): res = db.Media.find({}).count() client.close() logger.debug(res) return str(res)
# 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): update.message.reply_text('Hi! Use /status to check mongo status')
def help(bot, update): update.message.reply_text('Help!')
def echo(bot, update): update.message.reply_text(update.message.text)
def error(bot, update, error): logger.warn('Update "%s" caused error "%s"' % (update, error))
def status(bot, update): res = MediaCount() update.message.reply_text(res)
def main(): # Create the EventHandler and pass it your bot's token. updater = Updater("ur telegram token here")
# Get the dispatcher to register handlers dp = updater.dispatcher
# on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("status",status))
# on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors dp.add_error_handler(error)
# Start the Bot updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
if __name__ == '__main__': main()
Reference: https://neighborhood999.github.io/2016/07/19/Develop-telegram-bot/













