Slack Dispatcher¶
nautobot_chatops.dispatchers.slack
¶
Dispatcher implementation for sending content to Slack.
SlackDispatcher
¶
Bases: Dispatcher
Dispatch messages and cards to Slack.
Source code in nautobot_chatops/dispatchers/slack.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
command_prefix = settings.PLUGINS_CONFIG['nautobot_chatops']['slack_slash_command_prefix']
class-attribute
instance-attribute
¶
Prefix prepended to all commands, such as "/" or "!" in some clients.
__init__(*args, **kwargs)
¶
Init a SlackDispatcher.
Source code in nautobot_chatops/dispatchers/slack.py
actions_block(block_id, actions)
¶
bold(text)
¶
command_response_header(command, subcommand, args, description='information', image_element=None)
¶
Construct a consistently forwarded header including the command that was issued.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
str
|
Primary command string |
required |
subcommand |
str
|
Secondary command string |
required |
args |
list
|
of tuples, either (arg_name, human_readable_value, literal_value) or (arg_name, literal_value) |
required |
description |
str
|
Short description of what information is contained in the response |
'information'
|
image_element |
dict
|
As constructed by self.image_element() |
None
|
Source code in nautobot_chatops/dispatchers/slack.py
delete_message(response_url)
¶
get_prompt_from_menu_choices(choices, offset=0)
¶
Returns choices list to accommodate for Slack menu limits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
choices |
list
|
List of (display, value) tuples |
required |
offset |
int
|
If set, starts displaying choices at index location from all choices, as Slack only displays 100 options at a time |
0
|
Returns:
Name | Type | Description |
---|---|---|
choices |
list
|
List of (display, value) tuples accommodating for Slack menu limits |
Source code in nautobot_chatops/dispatchers/slack.py
hyperlink(text, url)
¶
image_element(url, alt_text='')
¶
Construct an image element that can be embedded in a block if URL is provided.
lookup_user_id_by_email(email)
classmethod
¶
Call out to Slack to look up a specific user ID by email.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
email |
str
|
Uniquely identifying email address of the user. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
(str, None) |
Source code in nautobot_chatops/dispatchers/slack.py
markdown_block(text)
¶
markdown_element(text)
¶
multi_input_dialog(command, sub_command, dialog_title, dialog_list)
¶
Provide several input fields on a single dialog.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
str
|
The top level command in use. (ex. net) |
required |
sub_command |
str
|
The command being invoked (ex. add-vlan) |
required |
dialog_title |
str
|
Title of the dialog box |
required |
dialog_list |
list
|
List of dictionaries containing the dialog parameters. See Example below. |
required |
Example
For a selection menu::
{
type: "select",
label: "label",
choices: [("display 1", "value1"), ("display 2", "value 2")],
default: ("display 1", "value1"),
confirm: False
}
For a text dialog::
{
type: "text",
label: "text displayed next to field"
default: "default-value",
optional: True
}
Dictionary Fields
- type: The type of object to create. Currently supported values: select, text
- label: A text descriptor that will be placed next to the field
- choices: A list of tuples which populates the choices in a dropdown selector
- default: (optional) Default choice of a select menu or initial value to put in a text field.
- confirm: (optional) If set to True, it will display a "Are you sure?" dialog upon submit.
- optional: (optional) If set to True, the field will return NoneType is not specified.
Source code in nautobot_chatops/dispatchers/slack.py
platform_lookup(item_type, item_name)
classmethod
¶
Call out to the chat platform to look up, e.g., a specific user ID by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item_type |
str
|
One of "organization", "channel", "user" |
required |
item_name |
str
|
Uniquely identifying name of the given item. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
(str, None) |
Source code in nautobot_chatops/dispatchers/slack.py
prompt_for_text(action_id, help_text, label, title='Your attention please!')
¶
Prompt the user to enter freeform text into a field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_id |
str
|
Identifier string to attach to the "submit" action. |
required |
help_text |
str
|
Markdown string to display as help text. |
required |
label |
str
|
Label text to display adjacent to the text field. |
required |
title |
str
|
Title to include on the modal dialog. |
'Your attention please!'
|
Source code in nautobot_chatops/dispatchers/slack.py
prompt_from_menu(action_id, help_text, choices, default=(None, None), confirm=False, offset=0)
¶
Prompt the user for a selection from a menu.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_id |
str
|
Identifier string to attach to the "submit" action. |
required |
help_text |
str
|
Markdown string to display as help text. |
required |
choices |
list
|
List of (display, value) tuples. |
required |
default |
tuple
|
Default (display, value) to pre-select. |
(None, None)
|
confirm |
bool
|
If True, prompt the user to confirm their selection (if the platform supports this). |
False
|
offset |
int
|
If set, starts displaying choices at index location from all choices, as Slack only displays 100 options at a time. |
0
|
Source code in nautobot_chatops/dispatchers/slack.py
select_element(action_id, choices, default=(None, None), confirm=False)
¶
Construct a basic selection menu with the given choices.
.. note:: Slack's select menu supports option groups, but others such as Adaptive Cards do not; we have to stick to the lowest common denominator, which means no groups.
TODO: maybe refactor this API so that Slack can use groups and other dispatchers just ignore the groups?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_id |
str
|
Identifying string to associate with this element |
required |
choices |
list
|
List of (display, value) tuples |
required |
default |
tuple
|
Default (display, value) to preselect |
(None, None)
|
confirm |
bool
|
If true (and the platform supports it), prompt the user to confirm their selection |
False
|
Source code in nautobot_chatops/dispatchers/slack.py
send_blocks(blocks, callback_id=None, modal=False, ephemeral=None, title='Your attention please!')
¶
Send a series of formatting blocks to the user/channel specified by the context.
Slack distinguishes between simple inline interactive elements and modal dialogs. Modals can contain multiple inputs in a single dialog; more importantly for our purposes, certain inputs (such as textentry) can ONLY be used in modals and will be rejected if we try to use them inline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks |
list
|
List of block contents as constructed by other dispatcher functions. |
required |
callback_id |
str
|
Callback ID string such as "command subcommand arg1 arg2". Required if |
None
|
modal |
bool
|
Whether to send this as a modal dialog rather than an inline block. |
False
|
ephemeral |
bool
|
Whether to send this as an ephemeral message (only visible to the targeted user). |
None
|
title |
str
|
Title to include on a modal dialog. |
'Your attention please!'
|
Source code in nautobot_chatops/dispatchers/slack.py
send_busy_indicator()
¶
Send a "typing" indicator to show that work is in progress.
Source code in nautobot_chatops/dispatchers/slack.py
send_error(message)
¶
Send an error message to the user/channel specified by the context.
send_exception(exception)
¶
Try to report an exception to the user.
Source code in nautobot_chatops/dispatchers/slack.py
send_image(image_path)
¶
Send an image as a file upload.
Source code in nautobot_chatops/dispatchers/slack.py
send_markdown(message, ephemeral=None)
¶
Send a Markdown-formatted text message to the user/channel specified by the context.
Source code in nautobot_chatops/dispatchers/slack.py
send_snippet(text, title=None, ephemeral=None)
¶
Send a longer chunk of text as a file snippet.
Source code in nautobot_chatops/dispatchers/slack.py
send_warning(message)
¶
Send a warning message to the user/channel specified by the context.
static_url(path)
¶
Return a url to access the static file.