#CREDITS TO TELEGRAM API, PYTHON-TELEGRAM-BOT API CREATORS, PYTHON DEVELOPERS, HUMAN BRAIN, UNIVERSE, GOD, AND SO ON. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters import logging import requests import telegram TOKEN = '231253517:AAEK8pNfftkHtmMLV3asrmAJKXUOb9LcnCc' start_text="Hi {0}.\nI'm a bot. I have to check the Nostale4DE server status.\nCheck /help for the available commands.\nHave a nice day." #START LOGGING logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO) #START DEFINING FUNCTIONS def start(bot, update): bot.sendMessage(chat_id=update.message.chat_id, text=start_text.format(update.message.from_user.first_name)) def status(bot, update): #SET UP VARIABLES FOR REQUESTS url = "http://nostale4de.com/serverstate.php" r = requests.get(url) raw_resp = r.text nice_resp = raw_resp.replace("<br>","\n") final_resp = "Server status: \n\n", nice_resp, "\nHave a nice day!" final_resp = "".join(final_resp) bot.sendMessage(chat_id=update.message.chat_id, text=final_resp.replace("Online","*Online*"),parse_mode=telegram.ParseMode.MARKDOWN) def aiuto(bot, update): bot.sendMessage(chat_id=update.message.chat_id, text="At the moment I support the following commands:\n\t/start - displays the welcome message\n\t/help - shows this message\n\t/status - checks server status") """ def unk(bot, update): bot.sendMessage(chat_ip=update.message.chat_id, text="_At the moment I can't understand this command, I'm sorry..._",parse_mode=telegram.ParseMode.MARKDOWN) """ #UPDATER AUTOMATICALLY FETCHES FOR UPDATES (WHEN THE BOT STARTS) AND PASS THEM TO THE DISPATCHER. #THE DISPATCHER WILL AUTOMATICALLY SORT UPDATES ACCORDING TO THE HANDLERS REGISTERED WITH add_handler FUNCTION, AND DELIVER THE UPDATES TO THE RIGHT FUNCTION #THE BOT ITEM IS AUTOMATICALLY CREATED BY THE UPDATER FUNCTION, AND IS "INTERNAL" TO THE UPDATER OBJECT updater = Updater(TOKEN) dispatcher = updater.dispatcher #REGISTER HANDLER TO SORT UPDATE, AND DELIVER UPDATES TO RIGHT FUNCTIONS dispatcher.add_handler(CommandHandler('start',start)) dispatcher.add_handler(CommandHandler('status',status)) dispatcher.add_handler(CommandHandler('help',aiuto)) #THIS MUST STAY LAST #dispatcher.add_handler(MessageHandler([Filters.command], unk)) #BOT STARTS FETCHING UPDATES AUTOMATICALLY updater.start_polling() updater.idle()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question