• R/O
  • SSH

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamewindowspythonphpguic翻訳omegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

Commit MetaInfo

Revision68d84481e0669d13990c458e5b4da7987dec5944 (tree)
Time2020-02-14 18:49:30
AuthorAlexander Larin
CommiterAlexander Larin

Log Message

Read user id from environment variable

Change Summary

Incremental Difference

diff -r 910cdf49d6df -r 68d84481e066 src/main.rs
--- a/src/main.rs Fri Feb 14 08:56:49 2020 +0300
+++ b/src/main.rs Fri Feb 14 12:49:30 2020 +0300
@@ -5,6 +5,7 @@
55
66 use futures::{Future, Stream};
77 use telegram_bot::*;
8+use telegram_bot::types::refs::UserId;
89 use tokio_core::reactor::Core;
910
1011 mod logger;
@@ -73,9 +74,25 @@
7374 false
7475 }
7576
77+fn try_get_creator_id() -> Option<UserId>
78+{
79+ // TODO: write it by and_then
80+ let res = std::env::var("CREATOR_ID");
81+ match res
82+ {
83+ Ok(s) => if let Ok(id) = s.parse()
84+ {
85+ Some(UserId::new(id))
86+ }
87+ else
88+ {
89+ None
90+ },
91+ Err(_) => None,
92+ }
93+}
94+
7695 fn main() {
77- let creator_id = telegram_bot::types::refs::UserId::new(211101859i64);
78-
7996 let mut core = Core::new().unwrap();
8097
8198 let token = std::env::var("TELEGRAM_BOT_TOKEN").unwrap();
@@ -86,6 +103,9 @@
86103
87104 let mut logger = logger::Logger::new();
88105
106+ let creator_id = try_get_creator_id();
107+
108+ if let Some(creator_id) = creator_id
89109 {
90110 let user = telegram_bot::chat::User {
91111 first_name: "".to_string(),
@@ -97,10 +117,9 @@
97117 };
98118 let chat = telegram_bot::chat::MessageChat::Private(user);
99119 api.spawn(SendMessage::new(chat, "Bot started"));
100-
101- logger.write("-----------------------------\nBot started");
102120 }
103121
122+ logger.write("-----------------------------\nBot started");
104123 // Fetch new updates via long poll method
105124 let message_process = api
106125 .stream()
@@ -108,7 +127,7 @@
108127 // If the received update contains a new message...
109128 if let UpdateKind::Message(message) = update.kind {
110129 if let MessageKind::Text { ref data, .. } = message.kind {
111- let logger_msg = if message.from.id == creator_id {
130+ let logger_msg = if Some(message.from.id) == creator_id {
112131 "Creator message".to_string()
113132 } else {
114133 format!(