Skip to content

Commit

Permalink
PARTIAL: rust rewrite
Browse files Browse the repository at this point in the history
reactions aren't working, looks like:
slack-rs/slack-rs#111
  • Loading branch information
bobrippling committed May 24, 2019
1 parent 7cdebb1 commit e8103f4
Show file tree
Hide file tree
Showing 11 changed files with 1,939 additions and 7 deletions.
1,646 changes: 1,646 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ps4bot"
version = "0.1.0"
authors = ["Rob Pilling <[email protected]>"]
edition = "2018"

[dependencies]
slack = "0.22.0" # https://slack-rs.github.io/slack-rs/slack/index.html
lazy_static = "1.3.0"
maplit = "1.0.1"
Empty file removed src/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions src/bots/bot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::msg::slackmessage::SlackMessage;

pub trait Bot
{
fn handle_message(&mut self, msg: &SlackMessage);
}
22 changes: 22 additions & 0 deletions src/bots/dumpbot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::bots::bot::Bot;
use crate::msg::slackmessage::SlackMessage;

pub struct DumpBot
{
}

impl Bot for DumpBot
{
fn handle_message(&mut self, msg: &SlackMessage)
{
println!("msg: {:?}", msg);
}
}

impl DumpBot
{
pub fn new() -> DumpBot
{
DumpBot {}
}
}
12 changes: 12 additions & 0 deletions src/bots/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod bot;
pub mod dumpbot;

//mod bots::doodlebot;
//mod bots::dumpbot;
//mod bots::logbot;
//mod bots::lunchbot;
//mod bots::memebot;
//mod bots::ps4bot;
//mod bots::shutupbot;
//mod bots::sportbot;
//mod bots::swiftbot;
7 changes: 0 additions & 7 deletions src/functional.py

This file was deleted.

80 changes: 80 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
extern crate slack;

#[macro_use]
extern crate maplit;

#[macro_use]
extern crate lazy_static;

mod config;
mod slackmonitor;
mod bots;
mod msg;

use slackmonitor::{SlackMonitor};
use bots::{bot::Bot, dumpbot::DumpBot /*,LunchBot,DoodleBot,SwiftBot,LogBot,ShutupBot,SportBot,MemeBot,PS4Bot*/};

fn usage() -> !
{
let argv0 = std::env::args().nth(0).unwrap();
eprintln!("Usage: {} channel(s...) -- bot(s...)", argv0);
std::process::exit(2);
}

fn bot_from_name(name: &str, _alias: &str) -> Option<Box<dyn Bot>>
{
match name {
"dumpbot" => Some(Box::new(DumpBot::new())),
/*
"lunchbot" => LunchBot,
"doodlebot" => DoodleBot,
"swiftbot" => SwiftBot,
"logbot" => LogBot,
"shutupbot" => ShutupBot,
"sportbot" => SportBot,
"memebot" => MemeBot,
"ps4bot" => PS4Bot,
*/
_ => None,
}
}

fn main() {
let args: Vec<String> = std::env::args().collect();

if args.len() < 4 {
usage();
}

let sep_index = args.iter().position(|i| i == "--")
.unwrap_or_else(|| usage());
let (channels, bots) = {
let (c, b) = args.split_at(sep_index);
(&c[1..], &b[1..])
};

let bot_token = config::SLACK_TOKEN;
let mut slack_monitor = SlackMonitor::new(bot_token.to_string());

for bot in bots {
let colon = bot.find(":");
let (bot_name, alias) = match colon {
Some(pos) => (&bot[..pos], &bot[pos+1..]),
None => (&bot[..], &bot[..])
};

let bot_obj = bot_from_name(bot_name, alias).unwrap_or_else(|| {
eprintln!("bot \"{}\" doesn't exist", bot_name);
std::process::exit(2);
});

if channels.len() == 1 && channels[0] == "*" {
slack_monitor.add_handler_for_all(bot_obj);
} else {
slack_monitor.add_handler_for_channel(bot_obj, channels.iter());
}
}

slack_monitor.run()
.expect("couldn't connect");
}
1 change: 1 addition & 0 deletions src/msg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod slackmessage;
11 changes: 11 additions & 0 deletions src/msg/slackmessage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Debug)]
pub struct SlackMessage<'a>
{
text: &'a str,
user: &'a str,
channel: &'a str, // ?
reply_to: Option<&'a str>,
bot_id: Option<&'a str>, // ?
when: u64, //?
thread_ts: Option<u64>, //?
}
151 changes: 151 additions & 0 deletions src/slackmonitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use std::collections::HashMap;

use slack::{
Event::{
self,
Message,
ReactionAdded,
ReactionRemoved,
},
RtmClient
};
use slack::{
Message::{
Standard,
MessageChanged,
MessageDeleted,
},
};

use crate::bots::bot::Bot;

pub struct SlackMonitor
{
token: String,
bots: Vec<Box<dyn Bot>>,
handlers: HashMap<String, Vec<usize>>,
allhandlers: Vec<usize>,
}

impl SlackMonitor
{
pub fn new(token: String) -> SlackMonitor
{
SlackMonitor {
token,
bots: Default::default(),
handlers: Default::default(),
allhandlers: Default::default(),
}
}

pub fn add_handler_for_channel<'i, It>(&mut self, bot: Box<dyn Bot>, channels: It)
where It: IntoIterator<Item=&'i String>
{
let index = self.bots.len();
self.bots.push(bot);

for channel in channels {
self.handlers.entry(channel.clone())
.or_insert(Default::default())
.push(index);
}
}

pub fn add_handler_for_all(&mut self, bot: Box<dyn Bot>)
{
let index = self.bots.len();
self.bots.push(bot);

self.allhandlers.push(index);
}

pub fn run(mut self) -> Result<(), slack::Error>
{
let token = std::mem::replace(&mut self.token, "".to_string());
RtmClient::login_and_run(&token[..], &mut self)
}
}

impl slack::EventHandler for SlackMonitor
{
fn on_close(&mut self, _client: &RtmClient) {
println!("disconnected"); // TODO: reconnect
}

fn on_connect(&mut self, _client: &RtmClient) {
println!("connected");
}

fn on_event(&mut self, client: &RtmClient, event: Event) {
println!("{:?}", event);
/*match event {
Message(msg) => match *msg {
Standard(msg) => {
/*let slack::Event::Message::MessageStandard {
channel, //:Option<String>,
edited, //:Option<MessageStandardEdited>, // { ts, user }
text, //:Option<String>,
ts, //:Option<String>,
user, //:Option<String>,
} = msg;*/
println!("got message: {:?}", msg);
},
MessageChanged(msg) => {
/*let MessageChanged {
channel, //:Option<String>,
hidden, //:Option<bool>,
message, //:Option<MessageMessageChangedMessage>, // edited: Option<MessageMessageChangedMessageEdited>, text: Option<String>, ts: Option<String>, ty: Option<String>, user: Option<String>,
subtype, //:Option<String>,
ts, //:Option<String>,
ty, //:Option<String>,
} = msg;*/
println!("got messagechanged: {:?}", msg);
},
MessageDeleted(msg) => {
/*let MessageDeleted {
channel, //:Option<String>,
deleted_ts, //:Option<String>,
hidden, //:Option<bool>,
subtype, //:Option<String>,
ts, //:Option<String>,
ty, //:Option<String>,
} = msg;*/
println!("got messagedeleted: {:?}", msg);
},
_ => (),
},
ReactionAdded {
ref user, //:String,
ref reaction, //:String,
ref item, //:Box<ListResponseItem>,
ref item_user, //:String,
ref event_ts, //:String,
} => {
println!("got reactionadded: {:?}", event);
},
ReactionRemoved {
ref user, //:String,
ref reaction, //:String,
ref item, //:Box<ListResponseItem>,
ref item_user, //:String,
ref event_ts, //:String,
} => {
println!("got reactionremoved: {:?}", event);
},
_ => (),
}*/

/*
for botindex in self.allhandlers {
self.bots[botindex].handle_event(event);
}
//self.handlers.get(event.
{
for botindex in
self.bots[botindex].handle_event(event);
}
*/
}
}

0 comments on commit e8103f4

Please sign in to comment.