codebuds / mattermost-publication-bundle
用于发布到 Mattermost Webhooks 的组件
0.6.0
2024-02-25 13:52 UTC
Requires
- php: ^8.3
- symfony/http-client: ^7.0
Requires (Dev)
- symfony/framework-bundle: ^7.0
README
配置
此组件允许您轻松地将文本发布到 Mattermost Webhook。
为了实现这一点,您需要通过在 symfony 中添加配置,例如,向 config/packages 添加 mattermost_publication.yaml
文件来配置
mattermost_publication: webhook_url: '%env(MATTERMOST_WEBHOOK_URL)%' username: 'My general username' channel: 'general-channel' icon_url: 'https://mysite.com/build/static/my_logo.webp'
并将 Webhook URL 添加到环境变量中
###> codebuds/mattermost-publication###
MATTERMOST_WEBHOOK_URL="http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx"
###< codebuds/mattermost-publication###
这将设置所有发布的通用配置。如果这些未设置,您至少需要确保将 Webhook URL 添加到您要发布的消息元素中。
用法
基本文本发布
您可以直接在控制器函数中通过将其作为参数添加来使用发布者(如果您已配置了通用的 webhook_url)
/** * @Route("/submit-donation") * @param MattermostPublication $publication * @return JsonResponse */ public function submitDonationForm(MattermostPublication $publication) { try { $publication->publish( "# donation made" ); } catch (\Exception $e) { return new JsonResponse(['message' => $e->getMessage()], 500); } }
在 eventSubscriber 中,您可以在构造函数中注入发布者
public function __construct(MattermostPublication $publication) { $this->publication = $publication; }
然后,在任意函数内部,您可以通过组件发布消息
try { $this->publication->publish( "# New contact form submission : " . "\n " . "**Name : **" . $message->getName() . "\n " . "**Email : **" . $message->getEmail() . "\n" . "### Message" . "\n" . $message->getMessage() ); } catch (\Exception $e) { return new JsonResponse(['message' => $e->getMessage()], 500); }
您还可以使用 twig 创建消息模板,如果您在 templates/mattermost/message.md.twig 中创建以下模板文件
# New contact form submission **Name : ** {{ message.name }} **Email : ** {{ message.email }} ### Message {{ message.message }}
您可以使用它并像以下这样使用它
$messageText = $this->twig->render('mattermost/message.md.twig', ['message' => $message]); $this->publication->publish($messageText);
配置的发布
由于可以发布简单的文本,因此还可以配置消息以使用所有可用的 Mattermost 入站 Webhook 功能。
为此,您必须创建一个 CodeBuds\MattermostPublicationBundle\Model\Message
对象,而不是只发布文本。
use CodeBuds\MattermostPublicationBundle\Model\Message as MMMessage; use CodeBuds\MattermostPublicationBundle\MattermostPublication; use Twig\Environment; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class MyController extends AbstractController { public function submitDonationForm(MattermostPublication $publication, Environment $twig) { $variable = "I am a variable"; try { $mmMessage = (new MMMessage()) ->setText($twig->render('mattermost/mymessage.md.twig', ['myVariable' => $variable])) ->setUsername('MyUsername') ->setChannel('MyChannel') ->setIconUrl('https://mysite.com/build/static/my_logo.webp') ->setWebhookUrl('http://otherwebhookurl'); $publication->publish($mmMessage); } catch (\Exception $e) { return $e->getMessage(); } } }
以这种方式操作将覆盖配置文件中的通用设置。如果在配置文件中设置了某些内容,并且未使用设置器,则通用值将在消息中。
如果您想发送直接消息并且入站 Webhook 在 Mattermost 中未锁定到某个频道,您可以设置频道为以 @
开头的用户名。
$publication->publish((new Message()) ->setText("Hello John !") ->setChannel("@john"));