84 lines
2.2 KiB
Rust
84 lines
2.2 KiB
Rust
use futures_util::future::TryFutureExt;
|
|
use serde::Serialize;
|
|
use serde_json::json;
|
|
use slack_api;
|
|
use std::env;
|
|
|
|
#[derive(Debug, Serialize)]
|
|
struct SlackProfile<'a> {
|
|
status_text: &'a str,
|
|
status_emoji: &'a str,
|
|
status_expiration: i32,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct SlackUser {
|
|
pub id: String,
|
|
pub name: String,
|
|
pub tz_label: String,
|
|
pub tz_offset: i32,
|
|
}
|
|
|
|
impl From<slack_api::User> for SlackUser {
|
|
fn from(user: slack_api::User) -> Self {
|
|
SlackUser {
|
|
id: user.id.unwrap_or_default(),
|
|
name: user.name.unwrap_or_default(),
|
|
tz_label: user.tz_label.unwrap_or_default(),
|
|
tz_offset: user.tz_offset.unwrap_or_default() as i32,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn set_status(
|
|
user_id: &str,
|
|
status_text: &str,
|
|
status_emoji: &str,
|
|
status_expiration: i32,
|
|
) -> Result<(), String> {
|
|
let client = slack_api::default_client().map_err(|e| e.to_string())?;
|
|
let api_key = env::var("SLACK_OAUTH_ACCESS_TOKEN").expect("SLACK_OAUTH_ACCESS_TOKEN");
|
|
let profile = SlackProfile {
|
|
status_text,
|
|
status_emoji,
|
|
status_expiration,
|
|
};
|
|
let encoded_profile = json!(profile).to_string();
|
|
let set_req = slack_api::users_profile::SetRequest {
|
|
user: Some(&user_id),
|
|
profile: Some(&encoded_profile),
|
|
name: None,
|
|
value: None,
|
|
};
|
|
|
|
// Returns invalid_user unless the Slack app was installed by a team admin
|
|
// AND the Slack account is on a paid plan. :cry:
|
|
slack_api::users_profile::set(&client, &api_key, &set_req)
|
|
.map_err(|e| e.to_string())
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn list_users() -> Result<Vec<SlackUser>, String> {
|
|
let client = slack_api::default_client().map_err(|e| e.to_string())?;
|
|
let api_key = env::var("SLACK_OAUTH_ACCESS_TOKEN").expect("SLACK_OAUTH_ACCESS_TOKEN");
|
|
let list_req = slack_api::users::ListRequest::default();
|
|
|
|
let resp = slack_api::users::list(&client, &api_key, &list_req)
|
|
.map_err(|e| e.to_string())
|
|
.await?;
|
|
|
|
let users = resp
|
|
.members
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(|m| {
|
|
println!("Member: {:?}", m);
|
|
m.into()
|
|
})
|
|
.collect::<Vec<SlackUser>>();
|
|
|
|
Ok(users)
|
|
}
|