1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
use serde_json::Value; use std::cell::RefCell; use bot::LineBot; use sources::LineSource; use messages::LineMessage; #[derive(Serialize, Deserialize, Clone)] #[serde(tag = "type", rename_all = "camelCase")] pub enum LineEventType { Unfollow, Leave, } #[derive(Serialize, Deserialize, Clone)] pub struct LineEvent { #[serde(flatten, rename = "type")] kind: LineEventType, timestamp: u64, source: LineSource, } impl LineEvent { pub fn new (kind: LineEventType, timestamp: u64, source: LineSource) -> LineEvent { LineEvent { kind, timestamp, source } } } #[derive(Serialize, Deserialize, Clone)] pub enum PostBackParams { Date { date: String }, Time { time: String }, Datetime { datetime: String }, } #[derive(Serialize, Deserialize, Clone)] pub struct PostBack { data: String, params: PostBackParams, } #[derive(Serialize, Deserialize, Clone)] pub enum BeaconEventType { Enter, Leave, Banner, } #[derive(Serialize, Deserialize, Clone)] pub struct Beacon { hwid: String, kind: BeaconEventType, } #[derive(Serialize, Deserialize, Clone)] pub struct Link { result: String, nonce: String, } #[derive(Serialize, Deserialize, Clone)] #[serde(tag = "type", rename_all = "camelCase")] pub enum ReplyableEventType { Message { message: LineMessage }, Postback { postback: Value }, Beacon { beacon: Beacon }, AccountLink { link: Link }, Follow, Join, } #[derive(Serialize, Deserialize, Clone)] pub struct ReplyableEvent { #[serde(flatten, rename = "type")] pub kind: ReplyableEventType, pub timestamp: u64, pub source: LineSource, #[serde(rename = "replyToken")] pub reply_token: String, } impl ReplyableEvent { pub fn new (kind: ReplyableEventType, timestamp: u64, source: LineSource, reply_token: &str) -> ReplyableEvent { ReplyableEvent { kind, timestamp, source, reply_token: String::from(reply_token), } } pub fn reply(&self, msg: Vec<LineMessage>, bot: LineBot) { let data = json!({ "replyToken": self.reply_token, "messages": msg }); bot.post("/message/reply", data, json!({})); } pub fn get_message(&self) -> Option<LineMessage> { match self.kind.clone() { ReplyableEventType::Message { message } => Some(message), _ => None } } }