help.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from collections.abc import Mapping, Sequence
  2. from sentry.integrations.slack.message_builder import SlackBlock
  3. from sentry.integrations.slack.message_builder.base.block import BlockSlackMessageBuilder
  4. from ..utils import logger
  5. UNKNOWN_COMMAND_MESSAGE = "Unknown command: `{command}`"
  6. HEADER_MESSAGE = "Here are the commands you can use. Commands not working? Re-install the app!"
  7. DM_COMMAND_HEADER = "*Direct Message Commands:*"
  8. CHANNEL_COMMANDS_HEADER = "*Channel Commands:*"
  9. CONTACT_HEADER = "*Contact:*"
  10. GENERAL_MESSAGE = "Just want to learn more about Sentry? Check out our <https://docs.sentry.io/product/integrations/notification-incidents/slack/|documentation>."
  11. DM_COMMANDS = {
  12. "link": "Link your Slack identity to your Sentry account to receive notifications. You'll also be able to perform actions in Sentry through Slack.",
  13. "unlink": "Unlink your Slack identity from your Sentry account.",
  14. "help": "View this list of commands.",
  15. }
  16. CHANNEL_COMMANDS = {
  17. "link team": "Get your Sentry team's issue alert notifications in the channel this command is typed in.",
  18. "unlink team": "Unlink a team from the channel this command is typed in.",
  19. }
  20. CONTACT_MESSAGE = "Let us know if you have feedback: ecosystem-feedback@sentry.io"
  21. SUPPORT_HEADER_MESSAGE = "Need support? Check out these resources:"
  22. SUPPORT_OPTIONS_MESSAGE = "• Want help for your particular case? <https://docs.sentry.io/support|File a ticket in Zendesk>. \n• Want to report an issue, request a feature, or get support? <https://github.com/getsentry/sentry/issues/new/choose|File a GitHub issue>. \n• Have feedback? Email feedback-ecosystem@sentry.io."
  23. DOCS_HEADER_MESSAGE = "Want to view documentation? Check out these resources:"
  24. DOCS_OPTIONS_MESSAGE = "• <https://docs.sentry.io/organization/integrations/|General Sentry integration docs> \n• <https://docs.sentry.io/organization/integrations/notification-incidents/slack/|Sentry Slack integration docs> \n• <https://sentry.slack.com/apps/A011MFBJEUU-sentry|Sentry Slack app>"
  25. def list_commands(commands: Mapping[str, str]) -> str:
  26. return "\n".join(
  27. (
  28. f"`/sentry {command}`: {description}"
  29. for command, description in sorted(tuple(commands.items()))
  30. )
  31. )
  32. DM_COMMANDS_MESSAGE = list_commands(DM_COMMANDS)
  33. CHANNEL_COMMANDS_MESSAGE = list_commands(CHANNEL_COMMANDS)
  34. class SlackHelpMessageBuilder(BlockSlackMessageBuilder):
  35. def __init__(self, command: str | None = None) -> None:
  36. super().__init__()
  37. self.command = command
  38. def get_header_blocks(self) -> Sequence[SlackBlock]:
  39. blocks = []
  40. if self.command and self.command != "help":
  41. logger.info("slack.event.unknown-command", extra={"command": self.command})
  42. blocks.append(
  43. self.get_markdown_block(UNKNOWN_COMMAND_MESSAGE.format(command=self.command))
  44. )
  45. blocks.append(self.get_markdown_block(HEADER_MESSAGE))
  46. return blocks
  47. def get_help_message(self) -> SlackBlock:
  48. return self._build_blocks(
  49. *self.get_header_blocks(),
  50. self.get_markdown_block(DM_COMMAND_HEADER),
  51. self.get_markdown_block(DM_COMMANDS_MESSAGE),
  52. self.get_markdown_block(CHANNEL_COMMANDS_HEADER),
  53. self.get_markdown_block(CHANNEL_COMMANDS_MESSAGE),
  54. self.get_markdown_block(CONTACT_HEADER),
  55. self.get_markdown_block(CONTACT_MESSAGE),
  56. self.get_divider(),
  57. self.get_markdown_block(GENERAL_MESSAGE),
  58. )
  59. def get_support_message(self) -> SlackBlock:
  60. return self._build_blocks(
  61. self.get_markdown_block(SUPPORT_HEADER_MESSAGE),
  62. self.get_markdown_block(SUPPORT_OPTIONS_MESSAGE),
  63. )
  64. def get_docs_message(self) -> SlackBlock:
  65. return self._build_blocks(
  66. self.get_markdown_block(DOCS_HEADER_MESSAGE),
  67. self.get_markdown_block(DOCS_OPTIONS_MESSAGE),
  68. )
  69. def build(self) -> SlackBlock:
  70. if self.command == "support":
  71. return self.get_support_message()
  72. elif self.command == "docs":
  73. return self.get_docs_message()
  74. else:
  75. return self.get_help_message()