Views

Working with templates

Sometimes, you need complex and big messages to be returned by your patterns. Bottery is able to render and respond messages using templates written in markdown.

On your project directory, create a folder named “templates”. Your templates should be kept inside this folder so Bottery can find them. For other configuration, please check the settings section.

Let’s create a template called hello.md:

Hello {{ user.first_name }}!

Welcome to **Bottery**

Good! Now we need our view to return a template instead of a simple string. To do this, we should user the method render.

Render is a default function that can receive the following parameters:

render(message, template_file, context={})

Here is an example using render without needing any extra content:

from bottery import Bottery
from bottery.message import render

bot = Bottery()

@bot.patterns.message('hello')
def hello(message):
    return render(message, 'hello.md')

Bottery already gives you by default the message user on you template context. If you need a new parameter on your template, just add a third parameter to the render function:

from bottery import Bottery
from bottery.message import render

bot = Bottery()


@bot.patterns.message('hello')
def hello(message):
    return render(message, 'hello.md',
                  {'question': 'interested, yet?'})