From b3fb42db813ad7a2be8470657ae9a1a7eb3da322 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 31 Mar 2018 10:15:16 +0200 Subject: [PATCH] added reply --- BeezleBot/BeezleBot.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/BeezleBot/BeezleBot.cpp b/BeezleBot/BeezleBot.cpp index 1037894..aa1ea07 100644 --- a/BeezleBot/BeezleBot.cpp +++ b/BeezleBot/BeezleBot.cpp @@ -14,29 +14,39 @@ int main(int argc, char **argv) DokuWiki wiki(settings.wikiUrl, settings.wikiUser, settings.wikiPassword); tgbot::LongPollBot bot(settings.telegramToken); - bot.callback([&wiki, &settings] (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 - && message.from->username != nullptr) + if (message.text != nullptr && message.from != nullptr + && message.from->username != nullptr) { - if (settings.telegramUsers.find(*message.from->username) - == settings.telegramUsers.end()) + 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; + 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()); + try + { + wiki.appendToPage("beezletest", wikiMessage.str()); + } + catch (std::runtime_error &e) + { + 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), "Stored to wiki"); } } }); @@ -51,4 +61,3 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } -