bytesnake / telebot

Write Telegram bots in Rust with Tokio and Futures
Other
212 stars 33 forks source link

Can't send messages from stream #26

Closed bemyak closed 6 years ago

bemyak commented 6 years ago

When I try to send message from steam nothing happens:

extern crate futures;
extern crate telebot;
extern crate tokio_core;

use telebot::RcBot;
use tokio_core::reactor::Core;
use futures::stream::Stream;
use std::env;
use futures::IntoFuture;

use telebot::functions::*;

fn main() {
    let mut lp = Core::new().unwrap();
    let bot = RcBot::new(lp.handle(), &env::var("BOT_TOKEN").unwrap()).update_interval(200);

    let stream = bot.get_stream().and_then(|(bot, update)| {
        // This prints an update
        println!("Received: {:#?}", update);
        let message = update.message.unwrap();
        // No message actually sent
        bot.message(message.chat.id, message.text.unwrap()).send();

        Ok(())
    });
    lp.run(stream.for_each(|_| Ok(())).into_future()).unwrap();
}

Please point me to what I'm missing. Thank you!

bytesnake commented 6 years ago

Hey, send only constructs the parsing and sending chain and returns a future which has to be resolved. You need to give it to the event loop. Either by a seperate handle or by returning it in the _andthen function.

let stream = bot.get_stream().and_then(|(bot, update)| {
        // ...
        bot.message(message.chat.id, message.text.unwrap()).send()
    });
bemyak commented 6 years ago

Oh, thank you, my bad!