Skip to content

Commit

Permalink
feat: add actions and fields to chat send (rockymadden#89)
Browse files Browse the repository at this point in the history
* feat: add actions and fields to chat send
  • Loading branch information
travismcchesney authored and rockymadden committed Nov 6, 2018
1 parent e95afea commit 46d2274
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ __Richly formatted chat example:__

```console
$ slack chat send \
--actions '{"type": "button", "style": "primary", "text": "See results", "url": "http://example.com"}' \
--author 'author' \
--author-icon 'https://assets-cdn.github.com/images/modules/logos_page/Octocat.png' \
--author-link 'https://github.com/rockymadden/slack-cli' \
--channel '#channel' \
--color good \
--fields '{"title": "Environment", "value": "snapshot", "short": true}' \
--footer 'footer' \
--footer-icon 'https://assets-cdn.github.com/images/modules/logos_page/Octocat.png' \
--image 'https://assets-cdn.github.com/images/modules/logos_page/Octocat.png' \
Expand Down
14 changes: 11 additions & 3 deletions src/slack
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ esac
# ARGUMENT AND OPTION PARSING #####################################################################
while (( "$#" )); do
case "${1}" in
--actions=*) actions=${1/--actions=/''} ; shift ;;
--actions*|-a*) actions=${2} ; shift ; shift ;;
--author-icon=*) authoricon=${1/--author-icon=/''} ; shift ;;
--author-icon*|-ai*) authoricon=${2} ; shift ; shift ;;
--author-link=*) authorlink=${1/--author-link=/''} ; shift ;;
Expand All @@ -55,6 +57,8 @@ while (( "$#" )); do
--count|-cn*) count=${2} ; shift ; shift ;;
--emoji=*) emoji=${1/--emoji=/''} ; shift ;;
--emoji*|-em*) emoji=${2} ; shift ; shift ;;
--fields=*) fields=${1/--fields=/''} ; shift ;;
--fields*|-flds*) fields=${2} ; shift ; shift ;;
--filename=*) filename=${1/--filename=/''} ; shift ;;
--filename*|-fln*) filename=${2} ; shift ; shift ;;
--filetype=*) filetype=${1/--filetype=/''} ; shift ;;
Expand Down Expand Up @@ -157,11 +161,13 @@ case "${cmd}${sub}" in
;;
chatsend)
[ -z "${channel}" ] && [ -z "${text}" ] && prompt='true'
[ -n "${prompt}" ] && [ -z "${actions}" ] && read -e -p 'Enter actions (e.g. {"type": “button", "style": "primary", "text": "my text", "url": "http://example.com"}, ...): ' actions
[ -n "${prompt}" ] && [ -z "${author}" ] && read -e -p 'Enter author name (e.g. slackbot): ' author
[ -n "${prompt}" ] && [ -z "${authoricon}" ] && read -e -p 'Enter author icon (e.g. a URL): ' authoricon
[ -n "${prompt}" ] && [ -z "${authorlink}" ] && read -e -p 'Enter author link (e.g. a URL): ' authorlink
[ -z "${channel}" ] && read -e -p 'Enter channel (e.g. #general): ' channel
[ -n "${prompt}" ] && [ -z "${color}" ] && read -e -p 'Enter color (e.g. good): ' color
[ -n "${prompt}" ] && [ -z "${fields}" ] && read -e -p 'Enter fields (e.g. {"title": "My Field Title", "value": "My field value", "short": true}, ...): ' fields
[ -n "${prompt}" ] && [ -z "${footer}" ] && read -e -p 'Enter footer (e.g. Hello footer!): ' footer
[ -n "${prompt}" ] && [ -z "${footericon}" ] && read -e -p 'Enter footer icon (e.g. a URL): ' footericon
[ -n "${prompt}" ] && [ -z "${image}" ] && read -e -p 'Enter image (e.g. a URL): ' image
Expand Down Expand Up @@ -216,10 +222,12 @@ esac

# COMMAND UTILITY FUNCTIONS #######################################################################
function attachify() {
[ -n "${actions}" ] && local _actions=", \"actions\": [${actions}]"
[ -n "${author}" ] && local _author=", \"author_name\": \"${author}\""
[ -n "${authoricon}" ] && local _authoricon=", \"author_icon\": \"${authoricon}\""
[ -n "${authorlink}" ] && local _authorlink=", \"author_link\": \"${authorlink}\""
[ -n "${color}" ] && local _color=", \"color\": \"${color}\""
[ -n "${fields}" ] && local _fields=", \"fields\": [${fields}]"
[ -n "${footer}" ] && local _footer=", \"footer\": \"${footer}\""
[ -n "${footericon}" ] && local _footericon=", \"footer_icon\": \"${footericon}\""
[ -n "${image}" ] && local _image=", \"image_url\": \"${image}\""
Expand All @@ -228,7 +236,7 @@ function attachify() {
[ -n "${_time}" ] && local __time=", \"ts\": \"${_time}\""
[ -n "${title}" ] && local _title=", \"title\": \"${title}\""
[ -n "${titlelink}" ] && local _titlelink=", \"title_link\": \"${titlelink}\""
local _json="${_author}${_authoricon}${_authorlink}${_color}${_footer}${_footericon}${_image}${_pretext}${_thumbnail}${__time}${_title}${_titlelink}"
local _json="${_author}${_authoricon}${_authorlink}${_color}${_footer}${_footericon}${_image}${_pretext}${_thumbnail}${__time}${_title}${_titlelink}${_fields}${_actions}"
[ -z "${_json}" ] && local _attachments="[{\"mrkdwn_in\": [\"pretext\"], \"fallback\": \"${text}\", \"pretext\": \"${text}\"}]"
[ -n "${_json}" ] && local _attachments="[{\"mrkdwn_in\": [\"fields\", \"pretext\", \"text\"], \"fallback\": \"${text}\", \"text\": \"${text}\"${_json}}]"

Expand Down Expand Up @@ -322,7 +330,7 @@ function help() {
echo " ${bin} chat send [<text> [channel]]"
echo ' [--author|-at <author>] [--author-icon|-ai <author-icon-url>]'
echo ' [--author-link|-al <author-link>] [--channel|-ch <channel>] [--color|-cl <color>]'
echo ' [--compact|-cp] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--compact|-cp] [--fields|-flds <fields>] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--footer-icon|-fi <footer-icon-url>] [--image|-im <image-url>] [--monochrome|-m]'
echo ' [--pretext|-pt <pretext>] [--text|-tx <text>] [--thumbnail|-th <thumbnail-url>]'
echo ' [--time|-tm <time>] [--title|-ti <title>] [--title-link|-tl <title-link>]'
Expand All @@ -331,7 +339,7 @@ function help() {
echo " ${bin} chat update [<text> [<timestamp> [channel]]]"
echo ' [--author|-at <author>] [--author-icon|-ai <author-icon-url>]'
echo ' [--author-link|-al <author-link>] [--channel|-ch <channel>] [--color|-cl <color>]'
echo ' [--compact|-cp] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--compact|-cp] [--fields|flds <fields>] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--footer-icon|-fi <footer-icon-url>] [--image|-im <image-url>] [--monochrome|-m]'
echo ' [--pretext|-pt <pretext>] [--text|-tx <text>] [--thumbnail|-th <thumbnail-url>]'
echo ' [--time|-tm <time>] [--timestamp|-ts <timestamp>] [--title|-ti <title>]'
Expand Down

0 comments on commit 46d2274

Please sign in to comment.