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"]
path = tgbot
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 "tgbot/bot.h"
#include "dokuwiki.h"
#include <iostream>
#include <sstream>
@ -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();

View File

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

View File

@ -10,12 +10,17 @@ Settings::Settings(int argc, char **argv)
{
namespace po = boost::program_options;
std::vector<std::string> telegramUserList;
po::options_description commandlineOptions("Allowed options");
commandlineOptions.add_options()
("help,h", "show this help message")
("config-file,f", po::value<std::string>(), "read options from config file")
("token,t", po::value<std::string>(&token)->required(), "Telegram bot token")
("users,u", po::value<std::vector<std::string>>(&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<std::string>(&token))
("Telegram.users", po::value<std::vector<std::string>>(&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)
{

View File

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

View File

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

1
libdokuwiki Submodule

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