From bd2e0f1254928a2e2fbb0a0f5103c7597baef279 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 28 Mar 2018 20:36:47 +0200 Subject: [PATCH] initial working (hacked) version --- .gitignore | 4 ++ .gitmodules | 4 ++ CMakeLists.txt | 4 ++ SimpleBot/CMakeLists.txt | 21 ++++++++ SimpleBot/SimpleBot.cpp | 106 +++++++++++++++++++++++++++++++++++++++ telegram-bot-api | 1 + 6 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 SimpleBot/CMakeLists.txt create mode 100644 SimpleBot/SimpleBot.cpp create mode 160000 telegram-bot-api diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9d39e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +_build +tags +.ycm_extra_conf.py +*.swp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ba237a5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "telegram-bot-api"] + path = telegram-bot-api + url = git@github.com:mandlm/telegram-bot-api + branch = cmake-submodule diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..eb3ef6f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.5) + +add_subdirectory(telegram-bot-api) +add_subdirectory(SimpleBot) diff --git a/SimpleBot/CMakeLists.txt b/SimpleBot/CMakeLists.txt new file mode 100644 index 0000000..ac7cfb4 --- /dev/null +++ b/SimpleBot/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.5) + +find_package(Boost 1.60.0 COMPONENTS program_options REQUIRED) + +project(SimpleBot LANGUAGES CXX) + +add_executable(${PROJECT_NAME} + SimpleBot.cpp +) + +target_link_libraries(${PROJECT_NAME} + PRIVATE + tgbot + Boost::program_options +) + +target_compile_features(${PROJECT_NAME} + PRIVATE + cxx_std_17 +) + diff --git a/SimpleBot/SimpleBot.cpp b/SimpleBot/SimpleBot.cpp new file mode 100644 index 0000000..24b64c2 --- /dev/null +++ b/SimpleBot/SimpleBot.cpp @@ -0,0 +1,106 @@ +#include "tgbot/bot.h" + +#include + +#include +#include +#include + +static const std::set knownUsers = { "mandlm" }; + +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(), "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 fullName(const tgbot::types::User &user) +{ + std::ostringstream fullName; + fullName << user.firstName; + + if (user.lastName != nullptr) + { + fullName << " " << *user.lastName; + } + + return fullName.str(); +} + +void replyUserUnknown(const tgbot::types::Message &message, const tgbot::methods::Api &api) +{ + api.sendMessage(std::to_string(message.chat.id), "Sorry, I don't know who you are."); +} + +void handleMessage(const tgbot::types::Message message, const tgbot::methods::Api &api) +{ + if (message.from == nullptr || knownUsers.find(*message.from->username) == knownUsers.cend()) + { + replyUserUnknown(message, api); + + return; + } + + std::cout << fullName(*message.from) + << " (" << *message.from->username << "): " + << (message.text != nullptr ? *message.text : "") << std::endl; + + api.sendMessage(std::to_string(message.chat.id), "You said:"); + if (message.text != nullptr) + { + api.sendMessage(std::to_string(message.chat.id), *message.text); + } + else + { + api.sendMessage(std::to_string(message.chat.id), "illegible things"); + } +} + +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 }); + + std::cout << "connected as " << bot.getMe().firstName << std::endl; + + bot.callback(handleMessage); + bot.start(); + } + catch (std::runtime_error &e) + { + std::cout << "exiting." << std::endl; + + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + diff --git a/telegram-bot-api b/telegram-bot-api new file mode 160000 index 0000000..286e252 --- /dev/null +++ b/telegram-bot-api @@ -0,0 +1 @@ +Subproject commit 286e252e99a882b2a77960132da19a12d2222341