ec实验室 / mattermost-bot
此包的最新版本(dev-master)没有可用的许可证信息。
使用PHP实现入站Webhooks、出站Webhooks和Slash命令的类集合,用于Mattermost。
dev-master
2018-09-20 09:59 UTC
Requires
- php: >=7.1.0
This package is not auto-updated.
Last update: 2024-09-29 05:01:10 UTC
README
Mattermost PHP 是一个工具集合,帮助您开发Bots、Webhooks或其他Mattermost应用程序。它提供代码,可以轻松地发送带有附件、动作(按钮)等复杂消息到MM,接收并解析Webhooks,并提供一个简单的包装器来制作机器人。同时,还在开发API连接器(通过Pull request提供帮助将受到欢迎)。
要求和安装
MattermostBot 可以通过composer安装。 $ composer require eclaboratorio/mattermost-bot
它需要php≤7.1
如何使用Mattermost机器人
这里您可以找到一些简单的示例
向Mattermost发送消息
//Webhook url provided when you create a new slash command $webhookUrl = 'https://yourMMserver.com/wooks/xxxxxxx'; $response = new \MattermostBot\Response(); $response->setText("This is an example text"); $response->setUsername("I'm a Bot"); $response->setChannel("@isoria"); //Create the attachment with it's options $attachment = new \MattermostBot\Attachment(); $attachment->setTitle("I'm an attachment"); $attachment->setText("I'm the content of the attachment"); $attachment->setTitleLink("https://www.elconfidencial.com"); $attachment->setColor("#ff0000"); $attachment->addFieldByText("One Field","With it's value",true); $attachment->addFieldByText("Second","yeah!",true); //Add a button $action = new \MattermostBot\AttachmentAction(); //The callback could be parsed with IncomingAction class $action->setUrl("https://example.com/webhookUrl/called-when-pressed"); $action->setName("Press me!"); //Values attached to context are sent to the button webhook and parsed by IncomingAction $action->attachToContext("param1",'value1'); $action->attachToContext("another_param",'asdf'); //Add the button to the attachment $attachment->addAction($action); //Attach the attachment and send $response->addAttachment($attachment); $response->sendIncomming($webhookUrl);
接收Slash命令
要解析入站Slash消息,只需创建一个新的IncomingSlash
$message = new \MattermostBot\IncomingSlash();
制作一个简单的Slash命令机器人
为了简化开发并实施一些安全最佳实践,有一个机器人包装器
您的Webhook URL
$message = new \MattermostBot\IncomingSlash(); $allowedTokens = ['1ifucdyn1pb1cdz1bk8dmkiqjc']; //Empty array means everybody could call the bot //The usernames wothout the @ $allowedUsers = ['myuser']; $bot = new TestBot($message, $allowedTokens, $allowedUsers); if ($bot->execute() === false) { echo "Bot execution fails"; } else { echo "It works!"; }
机器人文件
class TestBot extends \MattermostBot\BaseBot { //Any bot should implement a process function. //It is call after checking the user is allowed to run the command protected function process() { $response = new \MattermostBot\Response(); $response->setResponseType(\MattermostBot\Response::RESPONSE_IN_CHANNEL); $response->setUsername("TestBot"); $msg = "| Var | Value | | :------------ |:---------------| | Text | {$this->message->getText()} | | Token | {$this->message->getToken()} | | UserName | {$this->message->getUserName()} | | Command | {$this->message->getCommand()} | | ChannelId | {$this->message->getChannelId()} | | ChannelName | {$this->message->getChannelName()} | | TeamDomain | {$this->message->getTeamDomain()} | | UserId | {$this->message->getUserId()} |"; $response->setText($msg); $this->sendMessage($response); }