From f03dc86b2af24b133bc85f05941c9819ce6cd83b Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Sun, 31 Dec 2017 04:49:49 -0800 Subject: clean up DM support, add thing to send DMs --- src/commands/dm.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/commands/dm.rs (limited to 'src/commands/dm.rs') diff --git a/src/commands/dm.rs b/src/commands/dm.rs new file mode 100644 index 0000000..95f65b7 --- /dev/null +++ b/src/commands/dm.rs @@ -0,0 +1,53 @@ +use display::DisplayInfo; +use tw; +use ::Queryer; + +use tw::TweetId; + +use commands::Command; + +static DM_CREATE_URL: &str = "https://api.twitter.com/1.1/direct_messages/new.json"; + +pub static DM: Command = Command { + keyword: "dm", + params: 1, + exec: dm, + param_str: " ", + help_str: "Send DM to " +}; + +fn dm(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { + let user_profile = match tweeter.current_profile().map(|profile| profile.to_owned()) { + Some(profile) => profile, + None => { + display_info.status("To send a DM you must be an authenticated user.".to_owned()); + return; + } + }; + let mut text: String = line.trim().to_string(); + let text_bare = match text.find(" ") { + None => "".to_owned(), + Some(id_end_idx) => { + text.split_off(id_end_idx + 1) + } + }; + let dm_text = text_bare.trim(); + let handle_chars = text.trim().chars().collect::>(); + let normalized_handle = if handle_chars[0] == '@' { + handle_chars[1..].to_vec() + } else { + handle_chars + }.into_iter().collect::(); + + let encoded = ::url_encode(dm_text); + let result = match tweeter.current_profile() { + Some(user_profile) => { + queryer.do_api_post(&format!("{}?text={}&screen_name={}", DM_CREATE_URL, encoded, normalized_handle), &tweeter.app_key, &user_profile.creds) + }, + None => Err("No logged in user to DM as".to_owned()) + }; + match result { + Ok(_) => (), + Err(e) => display_info.status(e) + } +} -- cgit v1.1