initial working (hacked) version
This commit is contained in:
commit
bd2e0f1254
6 changed files with 140 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
_build
|
||||
tags
|
||||
.ycm_extra_conf.py
|
||||
*.swp
|
4
.gitmodules
vendored
Normal file
4
.gitmodules
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
[submodule "telegram-bot-api"]
|
||||
path = telegram-bot-api
|
||||
url = git@github.com:mandlm/telegram-bot-api
|
||||
branch = cmake-submodule
|
4
CMakeLists.txt
Normal file
4
CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
add_subdirectory(telegram-bot-api)
|
||||
add_subdirectory(SimpleBot)
|
21
SimpleBot/CMakeLists.txt
Normal file
21
SimpleBot/CMakeLists.txt
Normal file
|
@ -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
|
||||
)
|
||||
|
106
SimpleBot/SimpleBot.cpp
Normal file
106
SimpleBot/SimpleBot.cpp
Normal file
|
@ -0,0 +1,106 @@
|
|||
#include "tgbot/bot.h"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
static const std::set<std::string> 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<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>() };
|
||||
}
|
||||
|
||||
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 : "<no 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;
|
||||
}
|
||||
|
1
telegram-bot-api
Submodule
1
telegram-bot-api
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 286e252e99a882b2a77960132da19a12d2222341
|
Loading…
Reference in a new issue