Last active
September 28, 2019 07:01
-
-
Save yoshihitoh/462211a812b9717676cd92808dc09fcf to your computer and use it in GitHub Desktop.
Slack の中の人が教える Slack API ハンズオン in 福岡
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust-slack-app" | |
version = "0.1.0" | |
authors = ["yoshihitoh <yoshihito.arih@gmail.com>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
slack = "0.22" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use slack::api::MessageStandard; | |
use slack::{Event, EventHandler, Message, RtmClient}; | |
pub struct KeywordHandler { | |
keywords: Vec<String>, | |
} | |
impl From<Vec<String>> for KeywordHandler { | |
fn from(keywords: Vec<String>) -> Self { | |
KeywordHandler { keywords } | |
} | |
} | |
impl EventHandler for KeywordHandler { | |
fn on_event(&mut self, cli: &RtmClient, event: Event) { | |
dbg!(&event); | |
match event { | |
Event::Message(message) => self.handle_message(cli, *message), | |
_ => (), | |
} | |
} | |
#[allow(unused_variables)] | |
fn on_close(&mut self, cli: &RtmClient) { | |
println!("bye!"); | |
} | |
#[allow(unused_variables)] | |
fn on_connect(&mut self, cli: &RtmClient) { | |
println!("rust-slack bot is ready!"); | |
} | |
} | |
impl KeywordHandler { | |
fn handle_message(&self, cli: &RtmClient, message: Message) { | |
match message { | |
Message::Standard(standard) => self.handle_standard_message(cli, standard), | |
_ => (), | |
} | |
} | |
fn handle_standard_message(&self, cli: &RtmClient, standard: MessageStandard) { | |
// メッセージ本文にキーワードを含んでるかチェック | |
let text = standard.text.as_ref().map(|s| s.as_str()).unwrap_or(""); | |
if !self.keyword_matches(text) { | |
return; | |
} | |
// 反応する | |
let channel = standard.channel.unwrap_or("".to_string()); | |
let user = standard.user.unwrap_or("".to_string()); | |
let reply = format!("Hi <@{}>", user); // <@ユーザID> の形式で指定するとメンションになる | |
cli.sender().send_message(&channel, &reply); | |
} | |
fn keyword_matches(&self, text: &str) -> bool { | |
self.keywords.iter().any(|kw| text.contains(kw)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use slack::RtmClient; | |
mod handler; | |
use handler::KeywordHandler; | |
fn main() -> Result<(), slack::error::Error> { | |
let api_key = api_key(); | |
let mut handler = KeywordHandler::from(vec!["Hello".to_string()]); | |
RtmClient::login_and_run(&api_key, &mut handler) | |
} | |
fn api_key() -> String { | |
// APIトークンは環境変数 SLACK_API_TOKEN から取得する | |
env::var("SLACK_API_TOKEN").expect("Set API token to `SLACK_API_TOKEN`") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment