formatting

main
mandlm 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);
DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php",
settings.wikiUser, settings.wikiPassword);
DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php",
settings.wikiUser, settings.wikiPassword);
tgbot::LongPollBot bot(settings.telegramToken);
bot.callback(tgbot::utils::whenStarts, [](
const tgbot::types::Message message,
const tgbot::methods::Api &api,
const std::vector<std::string> args)
{
api.sendMessage(std::to_string(message.chat.id), "pong");
}, "/ping");
bot.callback(tgbot::utils::whenStarts,
[](const tgbot::types::Message message,
const tgbot::methods::Api &api,
const std::vector<std::string> args) {
api.sendMessage(std::to_string(message.chat.id), "pong");
},
"/ping");
bot.callback(tgbot::utils::whenStarts, [](
const tgbot::types::Message message,
const tgbot::methods::Api &api,
const std::vector<std::string> args)
{
api.sendMessage(std::to_string(message.chat.id),
bot.callback(tgbot::utils::whenStarts,
[](const tgbot::types::Message message,
const tgbot::methods::Api &api,
const std::vector<std::string> args) {
api.sendMessage(std::to_string(message.chat.id),
"Bot commands:\n"
"/ping \tsend life-sign\n"
"/help \tshow this message"
);
}, "/help");
"/help \tshow this message");
},
"/help");
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
&& message.from->username != nullptr)
&& message.from->username != nullptr)
{
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
{
std::ostringstream logMessage;
logMessage << message.from->firstName << " (" << *message.from->username << ")"
<< ":\\\\ " << *message.text;
logMessage << message.from->firstName << " ("
<< *message.from->username << ")"
<< ":\\\\ " << *message.text;
api.getLogger().info(logMessage.str());
std::ostringstream wikiMessage;
@ -61,8 +61,9 @@ int main(int argc, char **argv)
wiki.appendToPage("beezletest", wikiMessage.str());
std::ostringstream pageUrl;
pageUrl << settings.wikiUrl << "/doku.php?id=" << "beezletest";
api.sendMessage(std::to_string(message.chat.id),
pageUrl << settings.wikiUrl << "/doku.php?id="
<< "beezletest";
api.sendMessage(std::to_string(message.chat.id),
"Stored to wiki at " + pageUrl.str());
}
catch (std::runtime_error &e)
@ -70,7 +71,8 @@ int main(int argc, char **argv)
std::ostringstream reply;
reply << "Error writing to wiki: " << e.what();
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;
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")
("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")
;
commandlineOptions.add_options()("help,h", "show this help message")(
"config-file,f", po::value<std::string>(),
"read options from config file")("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;
po::store(po::command_line_parser(argc, argv).options(commandlineOptions).run(),
configuredOptions);
po::store(
po::command_line_parser(argc, argv).options(commandlineOptions).run(),
configuredOptions);
if (configuredOptions.find("help") != configuredOptions.cend())
{
@ -35,26 +38,31 @@ Settings::Settings(int argc, char **argv)
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;
fileOptions.add_options()
("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())
;
fileOptions.add_options()("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);
po::store(po::parse_config_file(configFile, fileOptions),
configuredOptions);
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"] });
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)
{

View File

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

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