Browse Source

first

master
alistair 3 years ago
commit
dffb67ed84
  1. 3
      .gitmodules
  2. 31
      CMakeLists.txt
  3. 75
      telegram_bot.cpp
  4. 1
      tgbot-cpp

3
.gitmodules vendored

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
[submodule "tgbot-cpp"]
path = tgbot-cpp
url = https://github.com/reo7sp/tgbot-cpp/

31
CMakeLists.txt

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
include(FetchContent)
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.7.3)
FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
cmake_minimum_required(VERSION 2.8.4)
project(echobot-submodule)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(Boost_USE_MULTITHREADED ON)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
add_subdirectory(tgbot-cpp)
add_executable(telegram_bog telegram_bot.cpp)
target_link_libraries(telegram_bog PRIVATE TgBot ${CMAKE_THREAD_LIBS_INIT}
${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES}
nlohmann_json::nlohmann_json)

75
telegram_bot.cpp

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>
#include <nlohmann/json.hpp>
#include <tgbot/tgbot.h>
using namespace TgBot;
using json = nlohmann::json;
namespace kare {
class db {
std::string filepath;
public:
db () {
}
};
};
int main() {
char *tok = getenv("TOKEN");
if (!tok) {
std::cout << "Need to set environment variable TOKEN" << std::endl;
exit(1);
}
std::string token {tok};
printf("Token: %s\n", token.c_str());
Bot bot(token);
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start")) {
return;
}
bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
});
signal(SIGINT, [](int s) {
printf("SIGINT got\n");
exit(0);
});
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
bot.getApi().deleteWebhook();
TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
longPoll.start();
}
} catch (std::exception& e) {
printf("error: %s\n", e.what());
}
return 0;
}

1
tgbot-cpp

@ -0,0 +1 @@ @@ -0,0 +1 @@
Subproject commit 9dae1b4ae78218a7ebd341f8b2952fe26affdf74
Loading…
Cancel
Save