diff --git a/connectors/tgbot-cpp/TgbotCppConnector.cpp b/connectors/tgbot-cpp/TgbotCppConnector.cpp new file mode 100644 index 0000000..fe40b1d --- /dev/null +++ b/connectors/tgbot-cpp/TgbotCppConnector.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +#include "connectors/tgbot-cpp/TgbotCppConnector.h" +#include "connectors/BotToken.h" +#include "connectors/Types.h" + +namespace MBFT +{ + TgbotCppConnector::TgbotCppConnector(void) + { + char token_buf[MBFT::BotToken::get_token_length() + 1]; + + if (!MBFT::BotToken::load_token(token_buf)) + { + std::cout << __FILE__ << ": Failed to load token\n"; + return; + } + + bot = std::make_unique(std::string(token_buf)); + + (*bot).getEvents().onAnyMessage( + [this](TgBot::Message::Ptr message) + { + inbound_message_callback(message); + } + ); + + inbound_thread = std::thread(TgbotCppConnector::inbound_message_thread, std::ref(*bot)); + outbound_thread = std::thread(TgbotCppConnector::outbound_message_thread, std::ref(*bot), std::ref(sendQ)); + } + + void TgbotCppConnector::inbound_message_thread(TgBot::Bot& bot) + { + TgBot::TgLongPoll longPoll(bot); + + for ( ; ; ) + { + std::cout << __FILE__ << ": Started polling\n"; + longPoll.start(); + } + } + + void TgbotCppConnector::inbound_message_callback(TgBot::Message::Ptr message) + { + std::shared_ptr new_msg = std::make_shared(); + MBFT::InboundMessage& msg_ref = *new_msg; + + msg_ref.id = message->messageId; + msg_ref.date = message->date; + msg_ref.text = message->text; + + msg_ref.from = std::make_shared(); + msg_ref.from->id = message->from->id; + msg_ref.from->firstName = message->from->firstName; + msg_ref.from->lastName = message->from->lastName; + msg_ref.from->username = message->from->username; + + msg_ref.chat = std::make_shared(); + msg_ref.chat->id = message->chat->id; + msg_ref.chat->title = message->chat->title; + + recvQ.push(new_msg); + } + + void TgbotCppConnector::outbound_message_thread(TgBot::Bot& bot, MBFT::ThreadSafeQueue>& q) + { + std::shared_ptr msg; + + for ( ; ; ) + { + msg = q.pop(); + bot.getApi().sendMessage(msg->target_chat_id, msg->text); + + std::this_thread::sleep_for(std::chrono::seconds(3)); + } + } + + std::shared_ptr TgbotCppConnector::receive(void) + { + return recvQ.pop(); + } + + void TgbotCppConnector::send(std::shared_ptr& msg) + { + sendQ.push(msg); + } +} diff --git a/connectors/tgbot-cpp/TgbotCppConnector.h b/connectors/tgbot-cpp/TgbotCppConnector.h new file mode 100644 index 0000000..b35fd13 --- /dev/null +++ b/connectors/tgbot-cpp/TgbotCppConnector.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include +#include + +#include "connectors/IConnector.h" +#include "external/tgbot-cpp/include/tgbot/tgbot.h" +#include "connectors/Types.h" +#include "core/inc/ThreadSafeQueue.h" + +namespace MBFT +{ + class TgbotCppConnector : public IConnector + { + private: + std::unique_ptr bot; + MBFT::ThreadSafeQueue> sendQ; + MBFT::ThreadSafeQueue> recvQ; + std::thread inbound_thread; + std::thread outbound_thread; + + void inbound_message_callback(TgBot::Message::Ptr message); + static void inbound_message_thread(TgBot::Bot& bot); + static void outbound_message_thread(TgBot::Bot& bot, MBFT::ThreadSafeQueue>& q); + public: + TgbotCppConnector(void); + virtual std::shared_ptr receive(void) override; + virtual void send(std::shared_ptr& msg) override; + }; +}