1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsでSlackにファイルをアップロードするCLIツールを作ってみた

Posted at

ターミナルで作業しているときにSlackにファイルをアップロードしたい場面って結構多くないですか?
そんなときにターミナルからコマンドでファイルをアップロードするシンプルなCLIツールを
Node.jsで作ってみました。

作ったもの

:sparkles: slaup

slaup 1.0.0

   USAGE

     slaup <filepath>

   ARGUMENTS

     <filepath>      Path to upload file      required

   OPTIONS

     -t <token>         Slack API Token                            optional
     -c <channels>      Comma-separated list of channel names      optional
     -f <filename>      Filename of file                           optional
     -m <comment>       Initial comment to add to file             optional
     -k <filetype>      A file type identifier                     optional

   GLOBAL OPTIONS

     -h, --help         Display help
     -V, --version      Display version
     --no-color         Disable colors
     --quiet            Quiet mode - only displays warn and error messages
     -v, --verbose      Verbose mode - will also output debug messages

使い方

インストール

$ npm install -g slaup

ファイルアップロード

-t <slack token> でSlackのAPIトークンを指定
-c <channels>でアップロード先のチャンネル名を指定します
チャンネル名はカンマ区切りで複数指定できるようになっています

$ slaup -t enter_your_slack_token -c general,hogehoge test.txt

SlackのAPIトークンはこちらから生成できます

またトークンとチャンネルは環境変数で設定することもできます

$ export SLAUP_SLACK_TOKEN=enter_your_slack_token
$ export SLAUP_CHANNELS=general,hogehoge
$ slaup test.txt

その他オプション

オプションとして以下も指定可能です

-f filename: アップロードするファイル名の指定
-m comment: コメント
-k filetype: スニペットのファイル種類を指定

使用モジュール

  • caporal : CLIツールのためのフレームワーク
  • chalk : ターミナルへの文字出力に色を付けるためのモジュール
  • ora : スピナー(ローディング中などにくるくるまわるやつ) を簡単に追加できるモジュール
  • request : 言わずもがな有名なHTTPリクエスト送るためのモジュール

chalkoravue-cli で使ってるのを見つけて使ってみました。
手軽にターミナル出力をリッチにできるのがいいですね :sparkles:

 まとめ

SlackにファイルをアップロードできるCLIツールは実は他にもあったりしますが、
Node.jsでCLIツールを作ってみたくて今回作ってみました。

色々なモジュールが揃っているのでさくっと作れていいですね。
今後もちょっとした機能追加などはしていきたいなと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?