diff options
author | Oleg Morozenkov <omorozenkov@gmail.com> | 2018-11-24 09:40:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-24 09:40:30 +0300 |
commit | 158c179beb3d06f110d665075d5e283e24592de6 (patch) | |
tree | 465d04c0ebbf573ddb0fa57ce2015bd44efd4ddf | |
parent | b35438dd77e4c2391391d7777c0a0b4cfd6e46f5 (diff) | |
parent | ba46cf2f3c9f253b84139f3b7229fe39fe5714ef (diff) |
Merge pull request #85 from ingjieye/master
Update CMakeLists.txt so tgbot-cpp can be used as a submodule
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | samples/echobot-submodule/CMakeLists.txt | 20 | ||||
-rw-r--r-- | samples/echobot-submodule/Dockerfile | 8 | ||||
-rw-r--r-- | samples/echobot-submodule/src/main.cpp | 46 |
5 files changed, 84 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a6d5c2e..c4f7195 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ set(LIB_LIST # building project add_library(${PROJECT_NAME} ${SRC_LIST}) +target_include_directories(${PROJECT_NAME} PUBLIC include) target_link_libraries(${PROJECT_NAME} ${LIB_LIST}) install(TARGETS ${PROJECT_NAME} DESTINATION lib) install(DIRECTORY include/ DESTINATION include) @@ -58,7 +58,7 @@ int main() { All other samples are located [here](samples). -## Library compilation +## Dependencies Firstly you need to install some dependencies such as Boost and build tools such as CMake. On Debian-based distibutives you can do it with these commands: ```sh @@ -66,14 +66,20 @@ sudo apt-get install g++ make binutils cmake libssl-dev libboost-system-dev ``` If you want to use curl-based http client `CurlHttpClient`, you also need to install `libcurl4-openssl-dev` package. -To compile the library execute this commands: +## Library installation + +If you want to install the library system-wide: + ```sh -cd /path/where/you/have/cloned/the/library/repository +git clone https://github.com/reo7sp/tgbot-cpp +cd tgbot-cpp cmake . make -j4 sudo make install ``` +Or you can treat this repository as a submodule of your project, for example, see [echobot-submodule](samples/echobot/CMakeLists.txt) + ## Specific library installation notes ### Docker diff --git a/samples/echobot-submodule/CMakeLists.txt b/samples/echobot-submodule/CMakeLists.txt new file mode 100644 index 0000000..fbb9087 --- /dev/null +++ b/samples/echobot-submodule/CMakeLists.txt @@ -0,0 +1,20 @@ +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) +find_package(CURL) +include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR}) +if (CURL_FOUND) + include_directories(${CURL_INCLUDE_DIRS}) + add_definitions(-DHAVE_CURL) +endif() + +add_subdirectory(tgbot-cpp) +add_executable(echobot-submodule src/main.cpp) + +target_link_libraries(echobot-submodule TgBot ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES}) diff --git a/samples/echobot-submodule/Dockerfile b/samples/echobot-submodule/Dockerfile new file mode 100644 index 0000000..a7aa61a --- /dev/null +++ b/samples/echobot-submodule/Dockerfile @@ -0,0 +1,8 @@ +FROM reo7sp/tgbot-cpp +MAINTAINER Oleg Morozenkov <a@reo7sp.ru> + +WORKDIR /usr/src/echobot +COPY . . +RUN cmake . +RUN make -j4 +CMD ./echobot diff --git a/samples/echobot-submodule/src/main.cpp b/samples/echobot-submodule/src/main.cpp new file mode 100644 index 0000000..966ae6f --- /dev/null +++ b/samples/echobot-submodule/src/main.cpp @@ -0,0 +1,46 @@ +#include <csignal> +#include <cstdio> +#include <cstdlib> +#include <exception> + +#include <tgbot/tgbot.h> + +using namespace std; +using namespace TgBot; + +int main() { + string token(getenv("TOKEN")); + 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 (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; +} |