|
| 1 | +from telegram import CommandHandlerWithHelpAndFather, CommandHandler |
| 2 | +class ExampleCommandHandler(CommandHandlerWithHelpAndFather): |
| 3 | + """This is an example how to use a CommandHandlerWithHelp or just a CommandHandler. |
| 4 | +
|
| 5 | + If You want to use a CommandHandler it is very easy. |
| 6 | + create a class which inherits a CommandHandler. |
| 7 | + create a method in this class which start with 'command_' and takes 1 argument: 'update' (which comes directly from |
| 8 | + getUpdate()). |
| 9 | + If you inherit CommandHandlerWithHelp it also creates a nice /help for you. |
| 10 | + """ |
| 11 | + def __init__(self, bot): # only necessary for a WithHelp |
| 12 | + super(ExampleCommandHandler, self).__init__(bot) |
| 13 | + self._help_title = 'Welcome this is a help file!' # optional |
| 14 | + self._help_before_list = """ |
| 15 | + Yeah here I explain some things about this bot. |
| 16 | + and of course I can do this in Multiple lines. |
| 17 | + """ # default is empty |
| 18 | + self._help_list_title = ' These are the available commands:' # optional |
| 19 | + self._help_after_list = ' These are some footnotes' # default is empty |
| 20 | + self.is_reply = True # default is True |
| 21 | + |
| 22 | + # only necessary if you want to override to default |
| 23 | + def _command_not_found(self, update): |
| 24 | + """Inform the telegram user that the command was not found.""" |
| 25 | + chat_id = update.message.chat.id |
| 26 | + reply_to = update.message.message_id |
| 27 | + message = "Sorry, I don't know how to do {command}.".format(command=update.message.text.split(' ')[0]) |
| 28 | + self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) |
| 29 | + |
| 30 | + # creates /test command. This code gets called when a telegram user enters /test |
| 31 | + def command_test(self, update): |
| 32 | + """ Test if the server is online. """ |
| 33 | + chat_id = update.message.chat.id |
| 34 | + reply_to = update.message.message_id |
| 35 | + message = 'Yeah, the server is online!' |
| 36 | + self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) |
| 37 | + |
| 38 | + # creates /parrot command |
| 39 | + def command_parrot(self, update): |
| 40 | + """ Says back what you say after the command""" |
| 41 | + chat_id = update.message.chat.id |
| 42 | + reply_to = update.message.message_id |
| 43 | + send = update.message.text.split(' ') |
| 44 | + message = update.message.text[len(send[0]):] |
| 45 | + if len(send) == 1: |
| 46 | + message = '...' |
| 47 | + self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) |
| 48 | + |
| 49 | + # creates /p command |
| 50 | + def command_p(self, update): |
| 51 | + """Does the same as parrot.""" |
| 52 | + return self.command_parrot(update) |
| 53 | + |
| 54 | + # this doesn't create a command. |
| 55 | + def another_test(self, update): |
| 56 | + """ This won't be called by the CommandHandler. |
| 57 | +
|
| 58 | + This is an example of a function that isn't a command in telegram. |
| 59 | + Because it didn't start with 'command_'. |
| 60 | + """ |
| 61 | + chat_id = update.message.chat.id |
| 62 | + reply_to = update.message.message_id |
| 63 | + message = 'Yeah, this is another test' |
| 64 | + self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) |
| 65 | + |
| 66 | + |
| 67 | +class Exampe2CommandHandler(CommandHandler): |
| 68 | + """ |
| 69 | + This is an example of a small working CommandHandler with only one command. |
| 70 | + """ |
| 71 | + def command_test(self, update): |
| 72 | + """ Test if the server is online. """ |
| 73 | + chat_id = update.message.chat.id |
| 74 | + reply_to = update.message.message_id |
| 75 | + message = 'Yeah, the server is online!' |
| 76 | + self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to) |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + import telegram as telegram |
| 80 | + try: |
| 81 | + from mytoken import token |
| 82 | + except: |
| 83 | + token = '' # use your own token here |
| 84 | + print ('token = ', token) |
| 85 | + Bot = telegram.Bot(token=token) |
| 86 | + test_command_handler = ExampleCommandHandler(Bot) |
| 87 | + test_command_handler.run() |
0 commit comments