From d82ab984e4e59ea03e5479a4c143f722e7009adc Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 30 Mar 2018 22:17:17 +0200 Subject: [PATCH] forward chat messages to DokuWiki --- .gitmodules | 3 +++ BeezleBot/BeezleBot.cpp | 43 ++++++++++++++++++++++------------------ BeezleBot/CMakeLists.txt | 1 + BeezleBot/settings.cpp | 25 +++++++++++++++++------ BeezleBot/settings.h | 10 +++++++--- CMakeLists.txt | 1 + libdokuwiki | 1 + 7 files changed, 56 insertions(+), 28 deletions(-) create mode 160000 libdokuwiki diff --git a/.gitmodules b/.gitmodules index 32552d9..088f30d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "tgbot"] path = tgbot url = git@github.com:mandlm/telegram-bot-api +[submodule "libdokuwiki"] + path = libdokuwiki + url = git@github.com:mandlm/libdokuwiki diff --git a/BeezleBot/BeezleBot.cpp b/BeezleBot/BeezleBot.cpp index 3d7e24c..1037894 100644 --- a/BeezleBot/BeezleBot.cpp +++ b/BeezleBot/BeezleBot.cpp @@ -1,5 +1,6 @@ #include "settings.h" #include "tgbot/bot.h" +#include "dokuwiki.h" #include #include @@ -10,29 +11,33 @@ int main(int argc, char **argv) { Settings settings(argc, argv); - std::cout << "token: " << settings.token << std::endl; - std::cout << "users: " << std::flush; - for (const auto &user : settings.users) - { - std::cout << user << " "; - } - std::cout << std::endl; + DokuWiki wiki(settings.wikiUrl, settings.wikiUser, settings.wikiPassword); - tgbot::LongPollBot bot(settings.token); - bot.callback([] (const tgbot::types::Message message, const tgbot::methods::Api &api) + tgbot::LongPollBot bot(settings.telegramToken); + bot.callback([&wiki, &settings] (const tgbot::types::Message message, + const tgbot::methods::Api &api) { - if (message.text != nullptr && message.from != nullptr) + if (message.text != nullptr + && message.from != nullptr + && message.from->username != nullptr) { - std::ostringstream logMessage; - logMessage << message.from->firstName; - - if (message.from->username != nullptr) - { - logMessage << " (" << *message.from->username << ")"; + if (settings.telegramUsers.find(*message.from->username) + == settings.telegramUsers.end()) + { + api.sendMessage(std::to_string(message.chat.id), "Unknown user!"); + } + else + { + std::ostringstream logMessage; + logMessage << message.from->firstName + << " (" << *message.from->username << ")" + << ": " << *message.text; + api.getLogger().info(logMessage.str()); + + std::ostringstream wikiMessage; + wikiMessage << "\n" << logMessage.str() << "\n"; + wiki.appendToPage("beezletest", wikiMessage.str()); } - - logMessage << ": " << *message.text; - api.getLogger().info(logMessage.str()); } }); bot.start(); diff --git a/BeezleBot/CMakeLists.txt b/BeezleBot/CMakeLists.txt index 31aee14..9d8a6c0 100644 --- a/BeezleBot/CMakeLists.txt +++ b/BeezleBot/CMakeLists.txt @@ -13,6 +13,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Boost::program_options tgbot + DokuWiki ) target_compile_features(${PROJECT_NAME} diff --git a/BeezleBot/settings.cpp b/BeezleBot/settings.cpp index 15684c7..0dbbdca 100644 --- a/BeezleBot/settings.cpp +++ b/BeezleBot/settings.cpp @@ -10,12 +10,17 @@ Settings::Settings(int argc, char **argv) { namespace po = boost::program_options; + std::vector telegramUserList; + po::options_description commandlineOptions("Allowed options"); commandlineOptions.add_options() ("help,h", "show this help message") ("config-file,f", po::value(), "read options from config file") - ("token,t", po::value(&token)->required(), "Telegram bot token") - ("users,u", po::value>(&users)->multitoken()->required(), "allowed Telegram users") + ("telegram-token", po::value(&telegramToken)->required(), "Telegram bot token") + ("telegram-users", po::value(&telegramUserList)->multitoken()->required(), "allowed Telegram users") + ("wiki-url", po::value(&wikiUrl)->required(), "DokuWiki xml-rpc url") + ("wiki-username", po::value(&wikiUser)->required(), "DokuWiki xml-rpc username") + ("wiki-password", po::value(&wikiPassword)->required(), "DokuWiki xml-rpc password") ; po::variables_map configuredOptions; @@ -34,16 +39,22 @@ Settings::Settings(int argc, char **argv) po::options_description fileOptions; fileOptions.add_options() - ("Telegram.token", po::value(&token)) - ("Telegram.users", po::value>(&users)->multitoken()) + ("Telegram.token", po::value(&telegramToken)) + ("Telegram.users", po::value(&telegramUserList)->multitoken()) + ("DokuWiki.url", po::value(&wikiUrl)->required()) + ("DokuWiki.username", po::value(&wikiUser)->required()) + ("DokuWiki.password", po::value(&wikiPassword)->required()) ; try { po::store(po::parse_config_file(configFile, fileOptions), configuredOptions); - configuredOptions.insert({ "token", configuredOptions["Telegram.token"] }); - configuredOptions.insert({ "users", configuredOptions["Telegram.users"] }); + configuredOptions.insert({ "telegram-token", configuredOptions["Telegram.token"] }); + configuredOptions.insert({ "telegram-users", configuredOptions["Telegram.users"] }); + configuredOptions.insert({ "wiki-url", configuredOptions["DokuWiki.url"] }); + configuredOptions.insert({ "wiki-username", configuredOptions["DokuWiki.username"] }); + configuredOptions.insert({ "wiki-password", configuredOptions["DokuWiki.password"] }); } catch (po::unknown_option &e) { @@ -55,6 +66,8 @@ Settings::Settings(int argc, char **argv) try { po::notify(configuredOptions); + + telegramUsers.insert(telegramUserList.begin(), telegramUserList.end()); } catch (po::required_option &e) { diff --git a/BeezleBot/settings.h b/BeezleBot/settings.h index 32b3c49..cf95a98 100644 --- a/BeezleBot/settings.h +++ b/BeezleBot/settings.h @@ -1,12 +1,16 @@ #pragma once #include -#include +#include struct Settings { - std::string token; - std::vector users; + std::string telegramToken; + std::set telegramUsers; + + std::string wikiUrl; + std::string wikiUser; + std::string wikiPassword; Settings(int argc, char **argv); }; diff --git a/CMakeLists.txt b/CMakeLists.txt index c5c0a29..f73dacd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ cmake_minimum_required(VERSION 3.5) add_subdirectory(tgbot) +add_subdirectory(libdokuwiki) add_subdirectory(BeezleBot) diff --git a/libdokuwiki b/libdokuwiki new file mode 160000 index 0000000..12ce383 --- /dev/null +++ b/libdokuwiki @@ -0,0 +1 @@ +Subproject commit 12ce38327fba57dcb3dfe873ae8e82f1fb7c53f3