forward chat messages to DokuWiki

This commit is contained in:
Michael Mandl 2018-03-30 22:17:17 +02:00
parent b192808a67
commit d82ab984e4
7 changed files with 56 additions and 28 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "tgbot"] [submodule "tgbot"]
path = tgbot path = tgbot
url = git@github.com:mandlm/telegram-bot-api url = git@github.com:mandlm/telegram-bot-api
[submodule "libdokuwiki"]
path = libdokuwiki
url = git@github.com:mandlm/libdokuwiki

View File

@ -1,5 +1,6 @@
#include "settings.h" #include "settings.h"
#include "tgbot/bot.h" #include "tgbot/bot.h"
#include "dokuwiki.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -10,29 +11,33 @@ int main(int argc, char **argv)
{ {
Settings settings(argc, argv); Settings settings(argc, argv);
std::cout << "token: " << settings.token << std::endl; DokuWiki wiki(settings.wikiUrl, settings.wikiUser, settings.wikiPassword);
std::cout << "users: " << std::flush;
for (const auto &user : settings.users)
{
std::cout << user << " ";
}
std::cout << std::endl;
tgbot::LongPollBot bot(settings.token); tgbot::LongPollBot bot(settings.telegramToken);
bot.callback([] (const tgbot::types::Message message, const tgbot::methods::Api &api) 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; if (settings.telegramUsers.find(*message.from->username)
logMessage << message.from->firstName; == settings.telegramUsers.end())
{
if (message.from->username != nullptr) api.sendMessage(std::to_string(message.chat.id), "Unknown user!");
{ }
logMessage << " (" << *message.from->username << ")"; 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(); bot.start();

View File

@ -13,6 +13,7 @@ target_link_libraries(${PROJECT_NAME}
PRIVATE PRIVATE
Boost::program_options Boost::program_options
tgbot tgbot
DokuWiki
) )
target_compile_features(${PROJECT_NAME} target_compile_features(${PROJECT_NAME}

View File

@ -10,12 +10,17 @@ Settings::Settings(int argc, char **argv)
{ {
namespace po = boost::program_options; namespace po = boost::program_options;
std::vector<std::string> telegramUserList;
po::options_description commandlineOptions("Allowed options"); po::options_description commandlineOptions("Allowed options");
commandlineOptions.add_options() commandlineOptions.add_options()
("help,h", "show this help message") ("help,h", "show this help message")
("config-file,f", po::value<std::string>(), "read options from config file") ("config-file,f", po::value<std::string>(), "read options from config file")
("token,t", po::value<std::string>(&token)->required(), "Telegram bot token") ("telegram-token", po::value(&telegramToken)->required(), "Telegram bot token")
("users,u", po::value<std::vector<std::string>>(&users)->multitoken()->required(), "allowed Telegram users") ("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; po::variables_map configuredOptions;
@ -34,16 +39,22 @@ Settings::Settings(int argc, char **argv)
po::options_description fileOptions; po::options_description fileOptions;
fileOptions.add_options() fileOptions.add_options()
("Telegram.token", po::value<std::string>(&token)) ("Telegram.token", po::value(&telegramToken))
("Telegram.users", po::value<std::vector<std::string>>(&users)->multitoken()) ("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 try
{ {
po::store(po::parse_config_file(configFile, fileOptions), configuredOptions); po::store(po::parse_config_file(configFile, fileOptions), configuredOptions);
configuredOptions.insert({ "token", configuredOptions["Telegram.token"] }); configuredOptions.insert({ "telegram-token", configuredOptions["Telegram.token"] });
configuredOptions.insert({ "users", configuredOptions["Telegram.users"] }); 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) catch (po::unknown_option &e)
{ {
@ -55,6 +66,8 @@ Settings::Settings(int argc, char **argv)
try try
{ {
po::notify(configuredOptions); po::notify(configuredOptions);
telegramUsers.insert(telegramUserList.begin(), telegramUserList.end());
} }
catch (po::required_option &e) catch (po::required_option &e)
{ {

View File

@ -1,12 +1,16 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector> #include <set>
struct Settings struct Settings
{ {
std::string token; std::string telegramToken;
std::vector<std::string> users; std::set<std::string> telegramUsers;
std::string wikiUrl;
std::string wikiUser;
std::string wikiPassword;
Settings(int argc, char **argv); Settings(int argc, char **argv);
}; };

View File

@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
add_subdirectory(tgbot) add_subdirectory(tgbot)
add_subdirectory(libdokuwiki)
add_subdirectory(BeezleBot) add_subdirectory(BeezleBot)

1
libdokuwiki Submodule

@ -0,0 +1 @@
Subproject commit 12ce38327fba57dcb3dfe873ae8e82f1fb7c53f3