kudubot_bindings/
lib.rs

1/*
2LICENSE:
3Copyright 2015-2017 Hermann Krumrey
4
5This file is part of kudubot.
6
7    kudubot is a chat bot framework. It allows developers to write
8    services for arbitrary chat services.
9
10    kudubot is free software: you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation, either version 3 of the License, or
13    (at your option) any later version.
14
15    kudubot is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with kudubot.  If not, see <http://www.gnu.org/licenses/>.
22LICENSE
23*/
24
25extern crate serde;
26#[macro_use]
27extern crate serde_json;
28#[macro_use]
29extern crate serde_derive;
30
31pub mod files;
32pub mod structs;
33
34use files::{read_from_file, write_json_to_file};
35use structs::Message;
36
37
38/// Loads a Message struct from a .json file
39///
40/// # Arguments
41///
42/// * `source` - The path to the source .json file
43///
44/// # Return value
45///
46/// Returns the generated Message struct
47pub fn load_message(source: &str) -> Message {
48    let data = read_from_file(source);
49    return serde_json::from_str(data.as_str()).unwrap()
50}
51
52
53/// Generates a reply Message struct for a given Message.
54/// For this, the receiver and sender are flipped and the body as well as
55/// the title are replaced.
56///
57/// # Arguments
58///
59/// * `message` - The Message to reply to
60/// * `title` - The message title to send
61/// * `body` - The message body to send
62///
63/// # Return value
64///
65/// Returns the generated reply message
66pub fn generate_reply(message: &Message, title: &str, body: &str) -> Message {
67
68    // Check if we need to reply to a group or not
69    let receiver = match message.sender_group.clone() {
70        None => message.sender.clone(),
71        Some(x) => x
72    };
73
74    return Message {
75        message_title: String::from(title),
76        message_body: String::from(body),
77        receiver: receiver.clone(),
78        sender: message.receiver.clone(),
79        sender_group: None,
80        timestamp: message.timestamp,
81    }
82}
83
84
85/// Writes a response to the is_applicable_to query by kudubot.
86///
87/// # Arguments
88///
89/// * `response_file` - The file to which to write to
90/// * `value` - The value to write into the response
91pub fn write_is_applicable_response(response_file: &str, value: bool) {
92    write_json_to_file(response_file, json!({ "is_applicable": value }))
93}
94
95/// Writes a reply response for the handle_message query by kudubot.
96///
97/// # Arguments
98///
99/// * `response_file` - The file to which to write to
100pub fn write_reply_response(response_file: &str) {
101    write_json_to_file(response_file, json!({ "mode": "reply" }))
102}