ontanj/slack-build

创建用于输入到 Slack 的数组

v1.0.0 2018-02-01 14:05 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:14:51 UTC


README

用于通过集成向 Slack 发送消息的辅助工具。Build 是一个静态类,可以帮助你以正确的方式添加消息的各个部分。开始构建时,传递一个空数组。在将其发送回 Slack 之前,你需要使用例如 json_encode() 将其转换为 json。

安装

composer require ontanj/slack-build

用法

静态类名为 Build,位于命名空间 SlackBuild 中。

要开始构建你的消息,将一个空数组传递给所需的函数。数组通过引用传递,所有内容都会按顺序添加。

在发送到 Slack 之前,需要将其转换为 json 字符串,例如使用 json_encode()

可用函数

/**
 * Adds an attachment to the message
 * @param array $data message to add the attachment to
 * @param string $fallback message to show if display is unavailable
 * @param string $callback_id id for interactive messages
 * @return int index of added attachment
 */
public static function add_attachment(array &$data, string $fallback, string $callback_id=null) : int

/**
 * Adds a button to a message
 * @param array $data message to add the button to
 * @param int $att_index index of the attachment to add to
 * @param string $name identifiable name for the button
 * @param string $text text shown on the button
 */
public static function add_button(array &$data, int $att_index, string $text, string $name)

/**
 * Adds a field to message
 * @param array $data message to add the field to
 * @param int $att_index index of the attachment to add to
 * @param string $title title of the field
 * @param string $value value of the field
 * @param bool $short whether the field should be short, true for short
 */
public static function add_field(array &$data, int $att_index, string $title, string $value, bool $short=true)

/**
 * Adds a list menu to a message
 * @param array $data message to add the menu to
 * @param int $att_index index of the attachment to add the menu to
 * @param string $name identifiable name for the menu
 * @param string $text text for the menu
 * @return int index of added menu
 */
public static function add_menu(array &$data, int $att_index, string $text, string $name) : int

/**
 * Adds a full list to a list menu, using the text also as value
 * @param array $data message to add the list to
 * @param int $att_index index of the attachment to add to
 * @param int $act_index index of the menu to add to
 * @param array $list array with list entries as element
 */
public static function add_menu_list(array &$data, int $att_index, int $act_index, array $texts, array $values)

/**
 * Adds an option to a list menu
 * @param array $data message to add the option to
 * @param int $att_index index of the attachment to add to
 * @param int $act_index index of the menu to add to
 * @param string $text text of the option
 * @param string $value key of the option
 */
public static function add_menu_option(array &$data, int $att_index, int $act_index, string $text, string $value)

/**
 * Adds a color to an attachment
 * @param array $data message to add the color to
 * @param int $att_index index of the attachment to add the color to
 * @param string $color the color, coded as hex-color with hashtag "#FFFFFF" or 
 * using predefined constants
 */
public static function attachment_color(array &$data, int $att_index, $color=self::random)

/**
 * Adds an attachment text to a message
 * @param array $data message to add the attachment text to
 * @param int $att_index index of the attachment to add to
 * @param string $title
 */
public static function attachment_text(array &$data, int $att_index, string $text)

/**
 * Adds an attachment title to a message
 * @param array $data message to add the attachment title to
 * @param int $att_index index of the attachment to add to
 * @param string $title
 */
public static function attachment_title(array &$data, int $att_index, string $title)

/**
 * Gets one of predefined constant colors
 * @param int $color
 * @return string
 */
private static function get_color(int $color)

/**
 * Adds top-level text to a message
 * @param array $data array to add the text to
 * @param string $text
 */
public static function message_text(array &$data, string $text)

/**
 * Sets the message to replace the original posted message 
 * @param array $data message to set
 * @param bool $replace true is replace, false is don't replace
 */
public static function replace_original(array &$data, bool $replace)

/**
 * Sets the message to delete the original message and post as new
 * @param array $data message to se
 * @param bool $delete true is delete, false is don't
 */
public static function delete_original(array &$data, bool $delete)

预定义颜色

const random = 0;
const black = 1;
const white = 2;
const yellow = 3;
const blue = 4;
const green = 5;
const red = 6;

未来

目标是包括更多函数。请随时贡献。