formatting

This commit is contained in:
Michael Mandl 2018-05-09 09:07:33 +02:00
parent 751fd47d2d
commit 60ea2b8572
4 changed files with 65 additions and 56 deletions

View file

@ -11,47 +11,47 @@ int main(int argc, char **argv)
{ {
Settings settings(argc, argv); Settings settings(argc, argv);
DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php", DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php",
settings.wikiUser, settings.wikiPassword); settings.wikiUser, settings.wikiPassword);
tgbot::LongPollBot bot(settings.telegramToken); tgbot::LongPollBot bot(settings.telegramToken);
bot.callback(tgbot::utils::whenStarts, []( bot.callback(tgbot::utils::whenStarts,
const tgbot::types::Message message, [](const tgbot::types::Message message,
const tgbot::methods::Api &api, const tgbot::methods::Api &api,
const std::vector<std::string> args) const std::vector<std::string> args) {
{ api.sendMessage(std::to_string(message.chat.id), "pong");
api.sendMessage(std::to_string(message.chat.id), "pong"); },
}, "/ping"); "/ping");
bot.callback(tgbot::utils::whenStarts, []( bot.callback(tgbot::utils::whenStarts,
const tgbot::types::Message message, [](const tgbot::types::Message message,
const tgbot::methods::Api &api, const tgbot::methods::Api &api,
const std::vector<std::string> args) const std::vector<std::string> args) {
{ api.sendMessage(std::to_string(message.chat.id),
api.sendMessage(std::to_string(message.chat.id),
"Bot commands:\n" "Bot commands:\n"
"/ping \tsend life-sign\n" "/ping \tsend life-sign\n"
"/help \tshow this message" "/help \tshow this message");
); },
}, "/help"); "/help");
bot.callback([&wiki, &settings](const tgbot::types::Message message, bot.callback([&wiki, &settings](const tgbot::types::Message message,
const tgbot::methods::Api &api) const tgbot::methods::Api &api) {
{
if (message.text != nullptr && message.from != nullptr if (message.text != nullptr && message.from != nullptr
&& message.from->username != nullptr) && message.from->username != nullptr)
{ {
if (settings.telegramUsers.find(*message.from->username) if (settings.telegramUsers.find(*message.from->username)
== settings.telegramUsers.end()) == settings.telegramUsers.end())
{ {
api.sendMessage(std::to_string(message.chat.id), "Unknown user!"); api.sendMessage(
std::to_string(message.chat.id), "Unknown user!");
} }
else else
{ {
std::ostringstream logMessage; std::ostringstream logMessage;
logMessage << message.from->firstName << " (" << *message.from->username << ")" logMessage << message.from->firstName << " ("
<< ":\\\\ " << *message.text; << *message.from->username << ")"
<< ":\\\\ " << *message.text;
api.getLogger().info(logMessage.str()); api.getLogger().info(logMessage.str());
std::ostringstream wikiMessage; std::ostringstream wikiMessage;
@ -61,8 +61,9 @@ int main(int argc, char **argv)
wiki.appendToPage("beezletest", wikiMessage.str()); wiki.appendToPage("beezletest", wikiMessage.str());
std::ostringstream pageUrl; std::ostringstream pageUrl;
pageUrl << settings.wikiUrl << "/doku.php?id=" << "beezletest"; pageUrl << settings.wikiUrl << "/doku.php?id="
api.sendMessage(std::to_string(message.chat.id), << "beezletest";
api.sendMessage(std::to_string(message.chat.id),
"Stored to wiki at " + pageUrl.str()); "Stored to wiki at " + pageUrl.str());
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
@ -70,7 +71,8 @@ int main(int argc, char **argv)
std::ostringstream reply; std::ostringstream reply;
reply << "Error writing to wiki: " << e.what(); reply << "Error writing to wiki: " << e.what();
api.getLogger().error(reply.str()); api.getLogger().error(reply.str());
api.sendMessage(std::to_string(message.chat.id), reply.str()); api.sendMessage(
std::to_string(message.chat.id), reply.str());
} }
} }
} }

View file

@ -13,19 +13,22 @@ Settings::Settings(int argc, char **argv)
std::vector<std::string> telegramUserList; 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>(),
("config-file,f", po::value<std::string>(), "read options from config file") "read options from config file")("telegram-token",
("telegram-token", po::value(&telegramToken)->required(), "Telegram bot token") po::value(&telegramToken)->required(),
("telegram-users", po::value(&telegramUserList)->multitoken()->required(), "allowed Telegram users") "Telegram bot token")("telegram-users",
("wiki-url", po::value(&wikiUrl)->required(), "DokuWiki xml-rpc url") po::value(&telegramUserList)->multitoken()->required(),
("wiki-username", po::value(&wikiUser)->required(), "DokuWiki xml-rpc username") "allowed Telegram users")("wiki-url", po::value(&wikiUrl)->required(),
("wiki-password", po::value(&wikiPassword)->required(), "DokuWiki xml-rpc password") "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;
po::store(po::command_line_parser(argc, argv).options(commandlineOptions).run(), po::store(
configuredOptions); po::command_line_parser(argc, argv).options(commandlineOptions).run(),
configuredOptions);
if (configuredOptions.find("help") != configuredOptions.cend()) if (configuredOptions.find("help") != configuredOptions.cend())
{ {
@ -35,26 +38,31 @@ Settings::Settings(int argc, char **argv)
if (configuredOptions.find("config-file") != configuredOptions.cend()) if (configuredOptions.find("config-file") != configuredOptions.cend())
{ {
std::ifstream configFile(configuredOptions["config-file"].as<std::string>()); std::ifstream configFile(
configuredOptions["config-file"].as<std::string>());
po::options_description fileOptions; po::options_description fileOptions;
fileOptions.add_options() fileOptions.add_options()("Telegram.token", po::value(&telegramToken))(
("Telegram.token", po::value(&telegramToken)) "Telegram.users", po::value(&telegramUserList)->multitoken())(
("Telegram.users", po::value(&telegramUserList)->multitoken()) "DokuWiki.url", po::value(&wikiUrl)->required())(
("DokuWiki.url", po::value(&wikiUrl)->required()) "DokuWiki.username", po::value(&wikiUser)->required())(
("DokuWiki.username", po::value(&wikiUser)->required()) "DokuWiki.password", po::value(&wikiPassword)->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({ "telegram-token", configuredOptions["Telegram.token"] }); configuredOptions.insert(
configuredOptions.insert({ "telegram-users", configuredOptions["Telegram.users"] }); {"telegram-token", configuredOptions["Telegram.token"]});
configuredOptions.insert({ "wiki-url", configuredOptions["DokuWiki.url"] }); configuredOptions.insert(
configuredOptions.insert({ "wiki-username", configuredOptions["DokuWiki.username"] }); {"telegram-users", configuredOptions["Telegram.users"]});
configuredOptions.insert({ "wiki-password", configuredOptions["DokuWiki.password"] }); 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)
{ {

View file

@ -12,6 +12,5 @@ struct Settings
std::string wikiUser; std::string wikiUser;
std::string wikiPassword; std::string wikiPassword;
Settings(int argc, char **argv); Settings(int argc, char **argv);
}; };

@ -1 +1 @@
Subproject commit a0ac1a79754aab02d66a2839e5405ec2161c29e6 Subproject commit b7c3c4c0426708e0547079f697ec92046b6a9202