From 282aee09e0dcd9f470df009e4e17be9047bfa8a1 Mon Sep 17 00:00:00 2001 From: Emile Date: Tue, 6 Jul 2021 00:54:08 +0100 Subject: [PATCH] wip unf work for telegram selector --- Cargo.lock | 408 ++++++++++++++++++++++++++++++++++---- Cargo.toml | 2 + src/main.rs | 41 ++-- src/model.rs | 21 +- src/selection/mod.rs | 1 + src/selection/telegram.rs | 73 +++++++ 6 files changed, 484 insertions(+), 62 deletions(-) create mode 100644 src/selection/telegram.rs diff --git a/Cargo.lock b/Cargo.lock index 56f6148..a49643b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,12 +76,12 @@ dependencies = [ "concurrent-queue", "futures-lite", "libc", - "log", + "log 0.4.14", "once_cell", "parking", "polling", "slab", - "socket2", + "socket2 0.4.0", "waker-fn", "winapi 0.3.9", ] @@ -150,11 +150,11 @@ dependencies = [ "futures-lite", "gloo-timers", "kv-log-macro", - "log", + "log 0.4.14", "memchr", "num_cpus", "once_cell", - "pin-project-lite", + "pin-project-lite 0.2.7", "pin-utils", "slab", "wasm-bindgen-futures", @@ -409,7 +409,7 @@ dependencies = [ "cookie", "failure", "idna 0.1.5", - "log", + "log 0.4.14", "publicsuffix", "serde", "serde_json", @@ -554,7 +554,7 @@ dependencies = [ "doc-comment", "hyper-old-types", "isolang", - "log", + "log 0.4.14", "reqwest", "serde", "serde_derive", @@ -585,7 +585,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" dependencies = [ "backtrace", - "version_check", + "version_check 0.9.3", ] [[package]] @@ -771,7 +771,7 @@ dependencies = [ "futures-io", "memchr", "parking", - "pin-project-lite", + "pin-project-lite 0.2.7", "waker-fn", ] @@ -820,7 +820,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite", + "pin-project-lite 0.2.7", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -895,12 +895,32 @@ dependencies = [ "futures 0.1.31", "http 0.1.21", "indexmap", - "log", + "log 0.4.14", "slab", "string", "tokio-io", ] +[[package]] +name = "h2" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e4728fd124914ad25e99e3d15a9361a879f6620f63cb56bbb08f95abb97a535" +dependencies = [ + "bytes 0.5.6", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.4", + "indexmap", + "slab", + "tokio 0.2.25", + "tokio-util", + "tracing", + "tracing-futures", +] + [[package]] name = "hashbrown" version = "0.9.1" @@ -950,12 +970,28 @@ dependencies = [ "tokio-buf", ] +[[package]] +name = "http-body" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" +dependencies = [ + "bytes 0.5.6", + "http 0.2.4", +] + [[package]] name = "httparse" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68" +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + [[package]] name = "hyper" version = "0.12.36" @@ -965,17 +1001,17 @@ dependencies = [ "bytes 0.4.12", "futures 0.1.31", "futures-cpupool", - "h2", + "h2 0.1.26", "http 0.1.21", - "http-body", + "http-body 0.1.0", "httparse", "iovec", "itoa", - "log", + "log 0.4.14", "net2", "rustc_version", "time", - "tokio", + "tokio 0.1.22", "tokio-buf", "tokio-executor", "tokio-io", @@ -983,7 +1019,31 @@ dependencies = [ "tokio-tcp", "tokio-threadpool", "tokio-timer", - "want", + "want 0.2.0", +] + +[[package]] +name = "hyper" +version = "0.13.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a6f157065790a3ed2f88679250419b5cdd96e714a0d65f7797fd337186e96bb" +dependencies = [ + "bytes 0.5.6", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.2.7", + "http 0.2.4", + "http-body 0.3.1", + "httparse", + "httpdate", + "itoa", + "pin-project", + "socket2 0.3.19", + "tokio 0.2.25", + "tower-service", + "tracing", + "want 0.3.0", ] [[package]] @@ -996,11 +1056,11 @@ dependencies = [ "bytes 0.4.12", "httparse", "language-tags", - "log", - "mime", + "log 0.4.14", + "mime 0.3.16", "percent-encoding 1.0.1", "time", - "unicase", + "unicase 2.6.0", ] [[package]] @@ -1011,11 +1071,24 @@ checksum = "3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f" dependencies = [ "bytes 0.4.12", "futures 0.1.31", - "hyper", + "hyper 0.12.36", "native-tls", "tokio-io", ] +[[package]] +name = "hyper-tls" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" +dependencies = [ + "bytes 0.5.6", + "hyper 0.13.10", + "native-tls", + "tokio 0.2.25", + "tokio-tls", +] + [[package]] name = "idna" version = "0.1.5" @@ -1105,6 +1178,8 @@ dependencies = [ "serde", "serde_json", "smol", + "telegram-bot", + "uuid 0.8.2", ] [[package]] @@ -1132,7 +1207,7 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" dependencies = [ - "log", + "log 0.4.14", ] [[package]] @@ -1162,6 +1237,15 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" +dependencies = [ + "log 0.4.14", +] + [[package]] name = "log" version = "0.4.14" @@ -1199,20 +1283,41 @@ dependencies = [ "autocfg 1.0.1", ] +[[package]] +name = "mime" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" +dependencies = [ + "log 0.3.9", +] + [[package]] name = "mime" version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "1.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216929a5ee4dd316b1702eedf5e74548c123d370f47841ceaac38ca154690ca3" +dependencies = [ + "mime 0.2.6", + "phf", + "phf_codegen", + "unicase 1.4.2", +] + [[package]] name = "mime_guess" version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" dependencies = [ - "mime", - "unicase", + "mime 0.3.16", + "unicase 2.6.0", ] [[package]] @@ -1237,7 +1342,7 @@ dependencies = [ "iovec", "kernel32-sys", "libc", - "log", + "log 0.4.14", "miow", "net2", "slab", @@ -1256,6 +1361,19 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "multipart" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136eed74cadb9edd2651ffba732b19a450316b680e4f48d6c79e905799e19d01" +dependencies = [ + "log 0.4.14", + "mime 0.2.6", + "mime_guess 1.8.8", + "rand 0.6.5", + "tempfile", +] + [[package]] name = "native-tls" version = "0.2.7" @@ -1264,7 +1382,7 @@ checksum = "b8d96b2e1c8da3957d58100b09f102c6d9cfdfced01b7ec5a8974044bb09dbd4" dependencies = [ "lazy_static", "libc", - "log", + "log 0.4.14", "openssl", "openssl-probe", "openssl-sys", @@ -1368,6 +1486,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-float" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3305af35278dd29f46fcdd139e0b1fbfae2153f0e5928b39b035542dd31e37b7" +dependencies = [ + "num-traits", +] + [[package]] name = "parking" version = "2.0.0" @@ -1457,8 +1584,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" dependencies = [ "siphasher", + "unicase 1.4.2", ] +[[package]] +name = "pin-project" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7509cc106041c40a4518d2af7a61530e1eed0e6285296a3d8c5472806ccc4a4" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c950132583b500556b1efd71d45b319029f2b71518d979fcc208e16b42426f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777" + [[package]] name = "pin-project-lite" version = "0.2.7" @@ -1485,7 +1639,7 @@ checksum = "92341d779fa34ea8437ef4d82d440d5e1ce3f3ff7f824aa64424cd481f9a1f25" dependencies = [ "cfg-if 1.0.0", "libc", - "log", + "log 0.4.14", "wepoll-ffi", "winapi 0.3.9", ] @@ -1535,7 +1689,7 @@ checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" dependencies = [ "bitflags", "memchr", - "unicase", + "unicase 2.6.0", ] [[package]] @@ -1781,23 +1935,23 @@ dependencies = [ "flate2", "futures 0.1.31", "http 0.1.21", - "hyper", - "hyper-tls", - "log", - "mime", - "mime_guess", + "hyper 0.12.36", + "hyper-tls 0.3.2", + "log 0.4.14", + "mime 0.3.16", + "mime_guess 2.0.3", "native-tls", "serde", "serde_json", "serde_urlencoded 0.5.5", "time", - "tokio", + "tokio 0.1.22", "tokio-executor", "tokio-io", "tokio-threadpool", "tokio-timer", "url 1.7.2", - "uuid", + "uuid 0.7.4", "winreg", ] @@ -1919,6 +2073,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-value" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a663f873dedc4eac1a559d4c6bc0d0b2c34dc5ac4702e105014b8281489e44f" +dependencies = [ + "ordered-float", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.126" @@ -2062,6 +2226,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "socket2" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "winapi 0.3.9", +] + [[package]] name = "socket2" version = "0.4.0" @@ -2110,6 +2285,36 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f13fc301d415a8cd4529ba679720c59f07369bcff573618a6e8d5afebefb6f3" +[[package]] +name = "telegram-bot" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc9552e972bcb551705fcad45bd0b86eca12a22379db36cdfa6d053e1a19b2de" +dependencies = [ + "bytes 0.5.6", + "futures 0.3.15", + "hyper 0.13.10", + "hyper-tls 0.4.3", + "multipart", + "telegram-bot-raw", + "tokio 0.2.25", + "tracing", + "tracing-futures", +] + +[[package]] +name = "telegram-bot-raw" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4fc120e2b85d639fc932a4aa7c1b6a5f1189fb86948d7a02037d1d8f1ef559" +dependencies = [ + "bytes 0.5.6", + "serde", + "serde-value", + "serde_derive", + "serde_json", +] + [[package]] name = "tempfile" version = "3.2.0" @@ -2168,6 +2373,23 @@ dependencies = [ "tokio-timer", ] +[[package]] +name = "tokio" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" +dependencies = [ + "bytes 0.5.6", + "fnv", + "futures-core", + "iovec", + "lazy_static", + "memchr", + "mio", + "pin-project-lite 0.1.12", + "slab", +] + [[package]] name = "tokio-buf" version = "0.1.1" @@ -2207,7 +2429,7 @@ checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" dependencies = [ "bytes 0.4.12", "futures 0.1.31", - "log", + "log 0.4.14", ] [[package]] @@ -2219,7 +2441,7 @@ dependencies = [ "crossbeam-utils 0.7.2", "futures 0.1.31", "lazy_static", - "log", + "log 0.4.14", "mio", "num_cpus", "parking_lot", @@ -2264,7 +2486,7 @@ dependencies = [ "crossbeam-utils 0.7.2", "futures 0.1.31", "lazy_static", - "log", + "log 0.4.14", "num_cpus", "slab", "tokio-executor", @@ -2282,6 +2504,30 @@ dependencies = [ "tokio-executor", ] +[[package]] +name = "tokio-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343" +dependencies = [ + "native-tls", + "tokio 0.2.25", +] + +[[package]] +name = "tokio-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" +dependencies = [ + "bytes 0.5.6", + "futures-core", + "futures-sink", + "log 0.4.14", + "pin-project-lite 0.1.12", + "tokio 0.2.25", +] + [[package]] name = "toml" version = "0.5.8" @@ -2291,6 +2537,55 @@ dependencies = [ "serde", ] +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + +[[package]] +name = "tracing" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" +dependencies = [ + "cfg-if 1.0.0", + "log 0.4.14", + "pin-project-lite 0.2.7", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-futures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" +dependencies = [ + "pin-project", + "tracing", +] + [[package]] name = "try-lock" version = "0.2.3" @@ -2318,7 +2613,7 @@ dependencies = [ "http 0.2.4", "httparse", "input_buffer", - "log", + "log 0.4.14", "native-tls", "rand 0.7.3", "sha-1", @@ -2338,13 +2633,22 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +[[package]] +name = "unicase" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" +dependencies = [ + "version_check 0.1.5", +] + [[package]] name = "unicase" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" dependencies = [ - "version_check", + "version_check 0.9.3", ] [[package]] @@ -2409,6 +2713,12 @@ dependencies = [ "rand 0.6.5", ] +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + [[package]] name = "value-bag" version = "1.0.0-alpha.7" @@ -2416,7 +2726,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd320e1520f94261153e96f7534476ad869c14022aee1e59af7c778075d840ae" dependencies = [ "ctor", - "version_check", + "version_check 0.9.3", ] [[package]] @@ -2425,6 +2735,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" + [[package]] name = "version_check" version = "0.9.3" @@ -2455,7 +2771,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" dependencies = [ "futures 0.1.31", - "log", + "log 0.4.14", + "try-lock", +] + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log 0.4.14", "try-lock", ] @@ -2489,7 +2815,7 @@ checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" dependencies = [ "bumpalo", "lazy_static", - "log", + "log 0.4.14", "proc-macro2", "quote", "syn", diff --git a/Cargo.toml b/Cargo.toml index 243c714..f06365c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ rand = "0.8.4" serde = "1.0.126" serde_json = "1.0.64" smol = "1.2.5" +telegram-bot = "0.8.0" +uuid = "0.8.2" diff --git a/src/main.rs b/src/main.rs index 878cbcc..38ad79e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use crate::{ use futures::StreamExt; use futures_timer::Delay; -use model::SampleModelExt; +use model::{EmptyModel, SampleModelExt}; mod bot; mod config; @@ -38,25 +38,26 @@ fn main() -> Result<(), Box> { } }; - let mut gpt_model = ConsoleSelector.filter( - model::GPTSampleModel::new( - cfg.python_path(), - cfg.gpt_code_path(), - vec![ - "generate_unconditional_samples.py".to_string(), - "--model_name".to_string(), - cfg.model_name(), - "--temperature".to_string(), - cfg.temperature(), - "--top_k".to_string(), - cfg.top_k(), - "--nsamples".to_string(), - "1".to_string(), - ], - ) - .into_stream() - .take(10), - ); + // let mut gpt_model = ConsoleSelector.filter( + // model::GPTSampleModel::new( + // cfg.python_path(), + // cfg.gpt_code_path(), + // vec![ + // "generate_unconditional_samples.py".to_string(), + // "--model_name".to_string(), + // cfg.model_name(), + // "--temperature".to_string(), + // cfg.temperature(), + // "--top_k".to_string(), + // cfg.top_k(), + // "--nsamples".to_string(), + // "1".to_string(), + // ], + // ) + // .into_stream() + // .take(10), + // ); + let mut gpt_model = ConsoleSelector.filter(EmptyModel.into_stream().take(5)); while let Some(Ok(sample)) = gpt_model.next().await { println!("{}", sample); diff --git a/src/model.rs b/src/model.rs index f66541e..147a57c 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,5 +1,11 @@ +use std::convert::Infallible; + use async_std::{io, process::Command}; -use futures::{future::BoxFuture, stream::BoxStream, Future, Stream, StreamExt, TryStreamExt}; +use futures::{ + future::{self, BoxFuture}, + stream::BoxStream, + Future, Stream, TryStreamExt, +}; pub trait SampleModel { type Error; @@ -8,6 +14,19 @@ pub trait SampleModel { fn get_sample(&self) -> Self::Sample; } +pub struct EmptyModel; + +impl SampleModel for EmptyModel { + type Error = Infallible; + type Sample = future::Ready>; + + fn get_sample(&self) -> Self::Sample { + future::ready(Ok(String::from( + "The quick brown fox jumps over the lazy dog.", + ))) + } +} + pub struct GPTSampleModel { python_command: String, command_working_path: String, diff --git a/src/selection/mod.rs b/src/selection/mod.rs index 5823c7d..e298808 100644 --- a/src/selection/mod.rs +++ b/src/selection/mod.rs @@ -1,6 +1,7 @@ use futures::{stream::BoxStream, Future, Stream, TryStreamExt}; mod console; +mod telegram; pub use console::ConsoleSelector; pub trait Selector { diff --git a/src/selection/telegram.rs b/src/selection/telegram.rs new file mode 100644 index 0000000..f1786a4 --- /dev/null +++ b/src/selection/telegram.rs @@ -0,0 +1,73 @@ +use async_std::future::pending; +use futures::{channel::oneshot::Sender, future::BoxFuture, lock::Mutex, Sink, StreamExt}; +use std::{collections::HashMap, sync::Arc}; +use telegram_bot::{ + self, requests, Api, CallbackQuery, ChatRef, InlineKeyboardButton, InlineKeyboardMarkup, + SendMessage, Update, UpdateKind, +}; +use uuid::Uuid; + +use super::Selector; + +pub struct TelegramSelector { + client: Arc, + chat_ref: Arc>, + pending: Arc>>>, +} + +impl TelegramSelector { + pub fn new(api: Arc, chat_ref: Arc>) -> Self { + let pending: Arc>>> = Arc::new(Mutex::new(HashMap::new())); + let stream = api.clone(); + let pending_clone = pending.clone(); + smol::spawn(async move { + let stream = stream.stream(); + while let Some(Ok(Update { + kind: + UpdateKind::CallbackQuery(CallbackQuery { + data: Some(query), .. + }), + .. + })) = stream.next().await + { + let uuid_bytes = query.as_bytes().get(0..32); + let bool_byte = query.as_bytes().get(32); + if let (Some(uuid), Some(keep)) = (uuid_bytes, bool_byte) { + let uuid = Uuid::parse_str(&String::from_utf8_lossy(uuid)); + if let Ok(uuid) = uuid { + if let Some(sender) = pending_clone.lock().await.get(&uuid) { + sender.send(match *keep as char { + 't' => true, + _ => false, + }); + } + } + } + } + }); + Self { + client: api, + chat_ref: chat_ref, + pending: pending, + } + } +} + +impl Selector for TelegramSelector { + type Error = telegram_bot::Error; + + type Response = BoxFuture<'static, Result>; + + fn review(&self, message: String) -> Self::Response { + let client = self.client.clone(); + let chat_ref = self.chat_ref.clone(); + let pending = self.pending.clone(); + Box::pin(async move { + let chat_ref = chat_ref.lock().await.clone(); + let message = SendMessage::new(chat_ref, message).reply_markup( + InlineKeyboardMarkup::new().add_row(vec![InlineKeyboardButton::callback("Keep")]), + ); + }) + } + // /setmain +}