扩展类以创建自定义功能

时间:2016-11-21 12:17:03

标签: php laravel laravel-5.3 slack-api

我目前正在使用这个slackbot软件包为Slack构建一个bot。

目前,它没有办法构建自定义附件,例如附加图像。

查看源代码,Question类为构建交互式问题做了很多繁重的工作 - 将它全部捆绑到一个准备发送给Slack的数组中。

我希望使用其中的大部分内容来构建一个可以向数组中添加image_url的类,但我以前从未做过类似的事情,并且不确定如何执行此操作或从何处开始

最终,我只是希望能够使用该包发送消息并包含图像附件。

从我可以拼凑的东西开始,我需要从这样的东西开始:

<?php

namespace //namespace;

use Mpociot\SlackBot\Question;

class Attachments extends Question
{
    //code to add image URL goes here

    /** 
    *   there is currently this function in the Question class 
    *   that builds the array where I need to add in
    *   'image_url => 'example.com/image_url'
    */
    public function toArray()
    {
        return [
            'text' => $this->text,
            'fallback' => $this->fallback,
            'callback_id' => $this->callback_id,
            'actions' => $this->buttons,
        ];
    }

}

任何人都可以帮我指出正确的方向或帮助我开始使用它吗?

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法从parent课程中获取所有功能:

parent::toArray();

在这种情况下,将执行扩展类中的整个函数。您始终可以将结果写入变量,如:

$parent = parent::toArray();

你可以这样回来:

return array (
        'image_url' => 'example.com/image_url',
) + parent::toArray();