This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
HelloBot/SimpleBot/SimpleBot.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

#include "MessageHandler.h"
2018-03-28 18:36:47 +00:00
#include "tgbot/bot.h"
#include <boost/program_options.hpp>
#include <string>
struct ProgramSettings
{
std::string token;
};
ProgramSettings getProgramSettings(int argc, char **argv)
{
namespace po = boost::program_options;
po::options_description optionsDescription("Allowed options");
optionsDescription.add_options()
("help", "show this help message")
("token,t", po::value<std::string>(), "Telegram bot token")
;
po::variables_map configuredOptions;
po::store(po::command_line_parser(argc, argv).options(optionsDescription).run(),
configuredOptions);
po::notify(configuredOptions);
if (configuredOptions.find("help") != configuredOptions.cend()
|| configuredOptions.find("token") == configuredOptions.cend())
{
std::cout << optionsDescription << std::endl;
throw std::runtime_error("invalid options");
}
return ProgramSettings{ configuredOptions["token"].as<std::string>() };
}
int main(int argc, char **argv)
{
try
{
auto settings = getProgramSettings(argc, argv);
std::cout << "Running bot... " << std::endl;
tgbot::LongPollBot bot(settings.token, { tgbot::types::UpdateType::MESSAGE });
2018-03-29 13:14:52 +00:00
bot.getLogger().info(std::string("connected as ") + bot.getMe().firstName);
2018-03-28 18:36:47 +00:00
MessageHandler messageHandler;
bot.callback([&messageHandler]
(const tgbot::types::Message message, const tgbot::methods::Api &api)
{
messageHandler.handle(message, api);
});
2018-03-28 18:36:47 +00:00
bot.start();
}
catch (std::runtime_error &e)
{
2018-03-29 13:14:52 +00:00
std::cout << "aborting." << std::endl;
2018-03-28 18:36:47 +00:00
return EXIT_FAILURE;
}
2018-03-29 13:14:52 +00:00
std::cout << "exiting." << std::endl;
2018-03-28 18:36:47 +00:00
return EXIT_SUCCESS;
}