LoginSignup
2
3

More than 5 years have passed since last update.

SlackAPIを使ってslash commandを送る方法

Posted at

SlackのAPIを使ってslash commandを受け取って処理する方法はあったが、
逆にAPIからslash commandを叩く情報がなかったのでメモ

問題

例えば、slack-ruby-client


Slack.configure do |config|
  config.token = ENV['SLACK_API_TOKEN']
end
client = Slack::Web::Client.new
client.chat_postMessage(channel: '#general', text: '/hoge', as_user: true)

と書いても、slackには/hogeと送信されるだけでslash commandを実行してくれない

解決策

chat.commandという非公式のメソッドがあったのでこれを使います ドキュメント

Slack.configure do |config|
  config.token = ENV['SLACK_API_TOKEN']
end
client = Slack::Web::Client.new
client.chat_command(channel:'HOGEEEE', command:'/hoge')

注意点としてchannelが#hogeではなくchannel IDで指定しないとダメそうなで気をつけてください
DMで送る場合も同様で、UserのIDではなくDMのchannel_IDを指定してください
これでslash commandが実行されるよ!やったね!

2
3
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
2
3