Quickstart#
A collection of small guides to get you going fast with the wrapper, aimed to teach the basics and how to best use the features of this wrapper.
Your first bot#
Let’s build a simple echo bot as a first example:
First of all, let’s be good programmers and let’s setup our main :)
def main():
pass
if __name__ == "__main__":
main()
To use the bot, you’ll need an Access Token. To generate an Access Token, you have to talk to @BotFather and follow a few simple steps (described here).
Now that you have a telegram bot token, let’s import what we’ll need: - The bot itself of course - A TextHandler, basically an object that handles text messages from the user - A Prompt, basically a ‘configurable function’ that allow the programmer to send a message.
from MateWrapper.bot import TelegramBot
from MateWrapper.handlers import TextHandler
from MateWrapper.prompts import Prompt
def main():
pass
if __name__ == "__main__":
main()
There, now that we have what we need, let’s put everything together. To instance a bot, just create a TelegamBot object:
bot = TelegramBot(token="your bot token", name="your bot name (this is entirely optional)")
then, to add an handler to our newly created bot we just need to call our bots add_handler method and pass in a text handler that itself will call a prompt:
bot.add_handler(TextHandler(Prompt("you said: {_text}")))
With this instruction we are basically telling the bot that, when it receives a text message, it should send a text message containing “you said: {_text}”.
Speaking of Prompts (docs here: prompts module), whenever you put a word between curly
brackets in the text, you are giving the prompt a context directive, these can be of many different types,
in our case we are using a special type of directive exclusive to prompts that will be replace with the
text that the user just sent.
For more info about context directives, see Context directives.
Back to our echo bot, it’s finished! Now all we have to do is start it, which you can with the following instruction:
bot.start_and_idle()
And there you go! We are done! Here’s how your entire bot should look like:
from MateWrapper.bot import TelegramBot
from MateWrapper.handlers import TextHandler
from MateWrapper.prompts import Prompt
def main():
bot = TelegramBot(token="your bot token", name="your bot name (this is entirely optional)")
bot.add_handler(TextHandler(Prompt("you said: {_text}")))
bot.start_and_idle()
if __name__ == "__main__":
main()
That wasn’t so bad, was it?
Now when you start the bot (assuming the token you used is valid) and write for exaple “hi” to it, it should respond with “you said: hi”.
3 lines of code for an echo bot is pretty good, but keep reading, it gets better ;)
Advanced usage#
This section will talk a bit about some of the more advanced features of MATE, for an example that uses most of them refer to The TODO list bot sample
Chains#
Chains are a way of chaining together multiple functions in a single handler, allowing you to create subroutines
that spare you code repetition. They can be put anywhere a callback should go (generally inside an Handler)
and you can even put chains inside other chains!
see Chain in generics module for more info
Custom Panels#
A bare bones implementation of the GenericPanel class that lets you customize
the panel’s prompt in detail and requires you to setup every single handler yourself.
A good example of this Type on panel’s usage can be found in the TODO list sample you
can find reference at the start of this paragraph.
Useful if you want to build an highly custom and dynamic panel inside your menu.
See CustomPanel in panels module for more info.
Decorating Panels#
All panels can be decorated (wrapped by another kind of panel) by using panel decorators; These classes can be used to easily add functionality to an entire panel without repeating yourself.
See panel_decorators module for more info.
Generating keyboards from lists#
Sometimes you need to generate dynamic keyboards from lists of things, since i found it to be a pretty common occurrence i built some helper functions that make doing it pretty easy:
get_keyboard_from_list:lets you generate a keyboard in a pretty standard way, automatically taking care of how the buttons are generated while letting you define a few parameters of the generated keyboard.
get_keyboard_from_list_custom_row:lets you get more “low level” with the keyboard generation, requiring you to pass in a function that will be used to generate a row of the keyboard.
See keyboards module for more info on both functions.

