Skip to content

Instantly share code, notes, and snippets.

@namikingsoft
Last active June 27, 2018 10:09
Show Gist options
  • Save namikingsoft/1f807b332847f844cc941f77cf8e59b6 to your computer and use it in GitHub Desktop.
Save namikingsoft/1f807b332847f844cc941f77cf8e59b6 to your computer and use it in GitHub Desktop.
[殴り書き] Slack 分報から日報用マークダウン生成 for MacOSX
#!/bin/sh -eu
# Usage: hunho2markdown.sh <2010-01-02>
# required: export SLACK_TOKEN="(e.g. https://api.slack.com/custom-integrations/legacy-tokens)"
# required: brew install pandoc
date="${1:-undefined}"
# slack variables
slack_domain="${SLACK_DOMAIN:-f**df****e}" # ****.slack.com
slack_userid="${SLACK_USERID:-U********}"
slack_channel_id="${SLACK_CHANNEL_ID:-C********}"
slack_channel_type="${SLACK_CHANNEL_TYPE:-channels}" # `channels` or `groups`
start_ts=$(
if [ "$date" = "undefined" ]
then date -j -f "%H:%M:%S" "00:00:00" "+%s"
else date -j -f "%Y-%m-%d %H:%M:%S" "${date} 00:00:00" "+%s"
fi
)
end_ts=$(
if [ "$date" = "undefined" ]
then date -j -f "%H:%M:%S" "23:59:59" "+%s"
else date -j -f "%Y-%m-%d %H:%M:%S" "${date} 23:59:59" "+%s"
fi
)
history_json=$(
curl --silent \
-X GET \
"https://slack.com/api/${slack_channel_type}.history?token=${SLACK_TOKEN}&channel=${slack_channel_id}&count=100&oldest=${start_ts}&latest=${end_ts}" \
| sed 's/\\n/<br>/g'
)
# for replace <LF> by sed
LF=$(printf '\\\012_')
LF=${LF%_}
paragraph() {
type="$1"
headline="$2"
exfilter="$3"
echo "## ${headline}"
echo
filter_loop=$(
echo "$history_json" \
| jq -r "
[
.messages
| reverse
| .[]
| ${exfilter}
| select(
(.text != null) and (.text != \"\") and
(.user != null) and (.user == \"${slack_userid}\")
)
]
| unique_by(.ts)
| .[]
| @base64
"
)
for row in $filter_loop; do
item_json=$(echo "$row" | base64 --decode)
ts=$(echo "$item_json" | jq -r .ts)
timestamp=$(echo "$ts" | sed 's|\..*$||')
timelabel=$(date -r "$timestamp" +"%H:%M:%S")
permalink="https://${slack_domain}.slack.com/messages/${slack_channel_id}/p$(echo "$ts" | sed 's|\.||')"
text=$(echo "$item_json" | jq -r .text | sed -E 's|<(https?:[^>]+)>|\[\1\]\(\1\)|g' | sed -E "s|<br>|$LF|g")
html=$(echo "${text}" | pandoc -f markdown+hard_line_breaks -t html | perl -0pe 's|^<p>||m') # TODO: avoid!! `| perl -0pe 's|^<p>||m'`
if [ "$type" = "timeline" ]
then echo "<dl><dt>${timelabel} <a href=\"${permalink}\">:slack:</a></dt><dd>${html}</dd></dl>"
else echo "$text"
fi
done
echo
}
paragraph_json=$(cat paragraph.json)
paragraph_length=$(echo "$paragraph_json" | jq length)
filter_other=""
for i in $(seq 0 $((paragraph_length - 1))); do
row=$(echo "$paragraph_json" | jq ".[$i]")
type=$(echo "$row" | jq -r .type)
title=$(echo "$row" | jq -r .title)
reaction=$(echo "$row" | jq -r .reaction)
filter="
(.reactions != null) and
(.reactions[].name == \"${reaction}\") and
(.reactions[].users[] == \"${slack_userid}\")
"
filter_other="${filter_other} or (${filter})"
paragraph "$type" "$title" "select(${filter})"
done
filter_other="
select(
(.reactions == null) or
($(echo "$filter_other" | perl -0pe 's|^ or ||m') | not)
)
"
echo "<details><summary>**クリックで生作業ログを展開**</summary><div>"
paragraph "timeline" "本日の作業ログ" "$filter_other"
echo "</div></details>"
[
{
"type": "plain",
"title": "TL;DR",
"reaction": "imakita"
},
{
"type": "plain",
"title": "朝会共有",
"reaction": "ohayou"
},
{
"type": "timeline",
"title": "作業内容まとめ",
"reaction": "github"
},
{
"type": "timeline",
"title": "参考にした記事",
"reaction": "link"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment