Debian Codegraph @

docs/user_guide/plugin_development/messaging.rst

raw
1 Messaging
2 =========
3
4
5 Returning multiple responses
6 ----------------------------
7
8 Often, with commands that take a long time to run, you may want to
9 be able to send some feedback to the user that the command is
10 progressing. Instead of using a single `return` statement you can
11 use `yield` statements for every line of output you wish to send to
12 the user.
13
14 In the following example, the output will be "Going to
15 sleep", followed by a 10 second wait period and "Waking up" in the end.
16
17 .. code-block:: python
18
19 from errbot import BotPlugin, botcmd
20 from time import sleep
21
22 class PluginExample(BotPlugin):
23 @botcmd
24 def longcompute(self, mess, args):
25 yield "Going to sleep"
26 sleep(10)
27 yield "Waking up"
28
29
30 Sending a message to a specific user or room
31 --------------------------------------------
32
33 Sometimes, you may wish to send a message to a specific user or a
34 groupchat, for example from pollers or on webhook events. You can do
35 this with :func:`~errbot.botplugin.BotPlugin.send`:
36
37 .. code-block:: python
38
39 self.send(
40 self.build_identifier("[email protected]/resource"),
41 "Boo! Bet you weren't expecting me, were you?",
42 )
43
44 :func:`~errbot.botplugin.BotPlugin.send` requires a valid
45 :class:`~errbot.backends.base.Identifier` instance to send to.
46 :func:`~errbot.botplugin.BotPlugin.build_identifier`
47 can be used to build such an identifier.
48 The format(s) supported by `build_identifier` will differ depending on which backend you are using.
49 For example, on Slack it may support `#channel` and `@user`,
50 for XMPP it includes `[email protected]/resource`, etc.
51
52
53 Templating
54 ----------
55
56 It's possible to send `Markdown
57 <http://daringfireball.net/projects/markdown/>`_ responses using `Jinja2
58 <http://jinja.pocoo.org/>`_ templates.
59
60 To do this, first create a directory called *templates* in the
61 directory that also holds your plugin's *.plug* file.
62
63 Inside this directory, you can place Markdown templates (with a
64 *.md* extension) in place of the content you wish to show. For
65 example this *hello.md*:
66
67 .. code-block:: jinja
68
69 Hello, {{name}}!
70
71 .. note::
72 See the Jinja2 `Template Designer Documentation
73 <http://jinja.pocoo.org/docs/templates/>`_ for more information on
74 the available template syntax.
75
76 Next, tell Errbot which template to use by specifying the `template`
77 parameter to :func:`~errbot.decorators.botcmd` (leaving off the
78 *.md* suffix).
79
80 Finally, instead of returning a string, return a dictionary where
81 the keys refer to the variables you're substituting inside the
82 template (`{{name}}` in the above template example):
83
84 .. code-block:: python
85
86 from errbot import BotPlugin, botcmd
87
88 class Hello(BotPlugin):
89 @botcmd(template="hello")
90 def hello(self, msg, args):
91 """Say hello to someone"""
92 return {'name': args}
93
94 It's also possible to use templates when using `self.send()`, but in
95 this case you will have to do the template rendering step yourself,
96 like so:
97
98 .. code-block:: python
99
100 from errbot import BotPlugin, botcmd
101 from errbot.templating import tenv
102
103 class Hello(BotPlugin):
104 @botcmd(template="hello")
105 def hello(self, msg, args):
106 """Say hello to someone"""
107 response = tenv().get_template('hello.md').render(name=args)
108 self.send(msg.frm, response)
109
110
111 Cards
112 -----
113
114 Errbot cards are a canned format for notifications. It is possible to use this format to map to some native format in
115 backends like Slack (Attachment).
116
117 Similar to a `self.send()` you can use :func:`~errbot.botplugin.BotPlugin.send_card` to send a card.
118
119 The following code demonstrate the various available fields.
120
121 .. code-block:: python
122
123 from errbot import BotPlugin, botcmd
124
125 class Travel(BotPlugin):
126 @botcmd
127 def hello_card(self, msg, args):
128 """Say a card in the chatroom."""
129 self.send_card(title='Title + Body',
130 body='text body to put in the card',
131 thumbnail='https://raw.githubusercontent.com/errbotio/errbot/master/docs/_static/errbot.png',
132 image='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
133 link='http://www.google.com',
134 fields=(('First Key','Value1'), ('Second Key','Value2')),
135 color='red',
136 in_reply_to=msg)
137
138 Trigger a callback with every message received
139 ----------------------------------------------
140
141 It's possible to add a callback that will be called on every message
142 sent either directly to the bot, or to a chatroom that the bot is
143 in:
144
145 .. code-block:: python
146
147 from errbot import BotPlugin
148
149 class PluginExample(BotPlugin):
150 def callback_message(self, mess):
151 if mess.body.find('cookie') != -1:
152 self.send(
153 mess.frm,
154 "What what somebody said cookie!?",
155 )