diff options
author | Oleg Morozenkov <m@oleg.rocks> | 2018-07-23 01:56:42 +0300 |
---|---|---|
committer | Oleg Morozenkov <m@oleg.rocks> | 2018-07-23 01:56:42 +0300 |
commit | d47ee877be5d1175bdc36f2d87881ddaf875a8e9 (patch) | |
tree | 7fd20cdc1236fe6b832ae980de12afd7071ebab9 /samples | |
parent | cea20d4078f2088dea0dd589f1cc9dd7ee22461b (diff) |
Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces
Diffstat (limited to 'samples')
-rw-r--r-- | samples/echobot-curl-client/CMakeLists.txt | 16 | ||||
-rw-r--r-- | samples/echobot-curl-client/Dockerfile | 8 | ||||
-rw-r--r-- | samples/echobot-curl-client/src/main.cpp | 48 | ||||
-rw-r--r-- | samples/echobot-webhook-server/CMakeLists.txt | 19 | ||||
-rw-r--r-- | samples/echobot-webhook-server/Dockerfile | 8 | ||||
-rw-r--r-- | samples/echobot-webhook-server/src/main.cpp | 42 | ||||
-rw-r--r-- | samples/echobot/CMakeLists.txt | 4 | ||||
-rw-r--r-- | samples/echobot/Dockerfile | 2 | ||||
-rw-r--r-- | samples/echobot/src/main.cpp | 88 | ||||
-rw-r--r-- | samples/inline-keyboard/CMakeLists.txt | 4 | ||||
-rw-r--r-- | samples/inline-keyboard/Dockerfile | 2 | ||||
-rw-r--r-- | samples/inline-keyboard/src/main.cpp | 92 | ||||
-rw-r--r-- | samples/photo/CMakeLists.txt | 4 | ||||
-rw-r--r-- | samples/photo/Dockerfile | 2 | ||||
-rw-r--r-- | samples/photo/src/main.cpp | 68 |
15 files changed, 267 insertions, 140 deletions
diff --git a/samples/echobot-curl-client/CMakeLists.txt b/samples/echobot-curl-client/CMakeLists.txt new file mode 100644 index 0000000..40e3200 --- /dev/null +++ b/samples/echobot-curl-client/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8.4) +project(echobot-curl-client) + +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} ${CURL_INCLUDE_DIRS}) +add_definitions(-DHAVE_CURL) + +add_executable(echobot-curl-client src/main.cpp) + +target_link_libraries(echobot-curl-client /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES}) diff --git a/samples/echobot-curl-client/Dockerfile b/samples/echobot-curl-client/Dockerfile new file mode 100644 index 0000000..a7aa61a --- /dev/null +++ b/samples/echobot-curl-client/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-curl-client/src/main.cpp b/samples/echobot-curl-client/src/main.cpp new file mode 100644 index 0000000..1f376a5 --- /dev/null +++ b/samples/echobot-curl-client/src/main.cpp @@ -0,0 +1,48 @@ +#include <csignal> +#include <cstdio> +#include <cstdlib> +#include <exception> + +#include <tgbot/tgbot.h> + +using namespace std; +using namespace TgBot; + +bool sigintGot = false; + +int main() { + string token(getenv("TOKEN")); + printf("Token: %s\n", token.c_str()); + + CurlHttpClient curlHttpClient; + + Bot bot(token, curlHttpClient); + 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"); + sigintGot = true; + }); + try { + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + + TgLongPoll longPoll(bot); + while (!sigintGot) { + printf("Long poll started\n"); + longPoll.start(); + } + } catch (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; +} diff --git a/samples/echobot-webhook-server/CMakeLists.txt b/samples/echobot-webhook-server/CMakeLists.txt new file mode 100644 index 0000000..49bcef1 --- /dev/null +++ b/samples/echobot-webhook-server/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.4) +project(echobot-webhook-server) + +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_executable(echobot-webhook-server src/main.cpp) + +target_link_libraries(echobot-webhook-server /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES}) diff --git a/samples/echobot-webhook-server/Dockerfile b/samples/echobot-webhook-server/Dockerfile new file mode 100644 index 0000000..a7aa61a --- /dev/null +++ b/samples/echobot-webhook-server/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-webhook-server/src/main.cpp b/samples/echobot-webhook-server/src/main.cpp new file mode 100644 index 0000000..7b5776a --- /dev/null +++ b/samples/echobot-webhook-server/src/main.cpp @@ -0,0 +1,42 @@ +#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()); + string webhookUrl(getenv("WEBHOOK_URL")); + printf("Webhook url: %s\n", webhookUrl.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); + }); + + try { + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + + TgWebhookTcpServer webhookServer(8080, bot); + + printf("Server starting\n"); + bot.getApi().setWebhook(webhookUrl); + webhookServer.start(); + } catch (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; +} diff --git a/samples/echobot/CMakeLists.txt b/samples/echobot/CMakeLists.txt index 0639d66..7019e8f 100644 --- a/samples/echobot/CMakeLists.txt +++ b/samples/echobot/CMakeLists.txt @@ -10,8 +10,8 @@ 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) + include_directories(${CURL_INCLUDE_DIRS}) + add_definitions(-DHAVE_CURL) endif() add_executable(echobot src/main.cpp) diff --git a/samples/echobot/Dockerfile b/samples/echobot/Dockerfile index 054c1c7..a7aa61a 100644 --- a/samples/echobot/Dockerfile +++ b/samples/echobot/Dockerfile @@ -1,7 +1,7 @@ FROM reo7sp/tgbot-cpp MAINTAINER Oleg Morozenkov <a@reo7sp.ru> -WORKDIR /tmp/echobot +WORKDIR /usr/src/echobot COPY . . RUN cmake . RUN make -j4 diff --git a/samples/echobot/src/main.cpp b/samples/echobot/src/main.cpp index 7c78112..802d318 100644 --- a/samples/echobot/src/main.cpp +++ b/samples/echobot/src/main.cpp @@ -1,27 +1,6 @@ -/* - * Copyright (c) 2015 Oleg Morozenkov - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include <signal.h> -#include <stdio.h> +#include <csignal> +#include <cstdio> +#include <cstdlib> #include <exception> #include <tgbot/tgbot.h> @@ -32,33 +11,36 @@ using namespace TgBot; bool sigintGot = false; int main() { - Bot bot("PLACE YOUR TOKEN HERE"); - 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"); - sigintGot = true; - }); - try { - printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); - - TgLongPoll longPoll(bot); - while (!sigintGot) { - printf("Long poll started\n"); - longPoll.start(); - } - } catch (exception& e) { - printf("error: %s\n", e.what()); - } - - return 0; + 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"); + sigintGot = true; + }); + try { + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + + TgLongPoll longPoll(bot); + while (!sigintGot) { + printf("Long poll started\n"); + longPoll.start(); + } + } catch (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; } diff --git a/samples/inline-keyboard/CMakeLists.txt b/samples/inline-keyboard/CMakeLists.txt index c149c0b..fe7c9bd 100644 --- a/samples/inline-keyboard/CMakeLists.txt +++ b/samples/inline-keyboard/CMakeLists.txt @@ -10,8 +10,8 @@ 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) + include_directories(${CURL_INCLUDE_DIRS}) + add_definitions(-DHAVE_CURL) endif() add_executable(inline-keyboard src/main.cpp) diff --git a/samples/inline-keyboard/Dockerfile b/samples/inline-keyboard/Dockerfile index 4598595..a65d5a1 100644 --- a/samples/inline-keyboard/Dockerfile +++ b/samples/inline-keyboard/Dockerfile @@ -1,7 +1,7 @@ FROM reo7sp/tgbot-cpp MAINTAINER Oleg Morozenkov <a@reo7sp.ru> -WORKDIR /tmp/inline-keyboard +WORKDIR /usr/src/inline-keyboard COPY . . RUN cmake . RUN make diff --git a/samples/inline-keyboard/src/main.cpp b/samples/inline-keyboard/src/main.cpp index 440f965..0976989 100644 --- a/samples/inline-keyboard/src/main.cpp +++ b/samples/inline-keyboard/src/main.cpp @@ -1,5 +1,6 @@ -#include <signal.h> -#include <stdio.h> +#include <csignal> +#include <cstdio> +#include <cstdlib> #include <exception> #include <tgbot/tgbot.h> @@ -10,46 +11,49 @@ using namespace TgBot; bool sigintGot = false; int main() { - Bot bot("PLACE YOUR TOKEN HERE"); - - // Thanks Pietro Falessi for code - InlineKeyboardMarkup::Ptr keyboard(new InlineKeyboardMarkup); - vector<InlineKeyboardButton::Ptr> row0; - InlineKeyboardButton::Ptr checkButton(new InlineKeyboardButton); - checkButton->text = "check"; - checkButton->callbackData = "check"; - row0.push_back(checkButton); - keyboard->inlineKeyboard.push_back(row0); - - bot.getEvents().onCommand("start", [&bot, &keyboard](Message::Ptr message) { - bot.getApi().sendMessage(message->chat->id, "Hi!", false, 0, keyboard); - }); - bot.getEvents().onCommand("check", [&bot, &keyboard](Message::Ptr message) { - string response = "ok"; - bot.getApi().sendMessage(message->chat->id, response, false, 0, keyboard, "Markdown"); - }); - bot.getEvents().onCallbackQuery([&bot, &keyboard](CallbackQuery::Ptr query) { - if (StringTools::startsWith(query->data, "check")) { - string response = "ok"; - bot.getApi().sendMessage(query->message->chat->id, response, false, 0, keyboard, "Markdown"); - } - }); - - signal(SIGINT, [](int s) { - printf("SIGINT got"); - sigintGot = true; - }); - try { - printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); - - TgLongPoll longPoll(bot); - while (!sigintGot) { - printf("Long poll started\n"); - longPoll.start(); - } - } catch (exception& e) { - printf("error: %s\n", e.what()); - } - - return 0; + string token(getenv("TOKEN")); + printf("Token: %s\n", token.c_str()); + + Bot bot(token); + + // Thanks Pietro Falessi for code + InlineKeyboardMarkup::Ptr keyboard(new InlineKeyboardMarkup); + vector<InlineKeyboardButton::Ptr> row0; + InlineKeyboardButton::Ptr checkButton(new InlineKeyboardButton); + checkButton->text = "check"; + checkButton->callbackData = "check"; + row0.push_back(checkButton); + keyboard->inlineKeyboard.push_back(row0); + + bot.getEvents().onCommand("start", [&bot, &keyboard](Message::Ptr message) { + bot.getApi().sendMessage(message->chat->id, "Hi!", false, 0, keyboard); + }); + bot.getEvents().onCommand("check", [&bot, &keyboard](Message::Ptr message) { + string response = "ok"; + bot.getApi().sendMessage(message->chat->id, response, false, 0, keyboard, "Markdown"); + }); + bot.getEvents().onCallbackQuery([&bot, &keyboard](CallbackQuery::Ptr query) { + if (StringTools::startsWith(query->data, "check")) { + string response = "ok"; + bot.getApi().sendMessage(query->message->chat->id, response, false, 0, keyboard, "Markdown"); + } + }); + + signal(SIGINT, [](int s) { + printf("SIGINT got\n"); + sigintGot = true; + }); + try { + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + + TgLongPoll longPoll(bot); + while (!sigintGot) { + printf("Long poll started\n"); + longPoll.start(); + } + } catch (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; } diff --git a/samples/photo/CMakeLists.txt b/samples/photo/CMakeLists.txt index bb52f3c..ae4b2eb 100644 --- a/samples/photo/CMakeLists.txt +++ b/samples/photo/CMakeLists.txt @@ -10,8 +10,8 @@ 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) + include_directories(${CURL_INCLUDE_DIRS}) + add_definitions(-DHAVE_CURL) endif() add_executable(photo src/main.cpp) diff --git a/samples/photo/Dockerfile b/samples/photo/Dockerfile index 2bdbe1f..ea9993a 100644 --- a/samples/photo/Dockerfile +++ b/samples/photo/Dockerfile @@ -1,7 +1,7 @@ FROM reo7sp/tgbot-cpp MAINTAINER Oleg Morozenkov <a@reo7sp.ru> -WORKDIR /tmp/photo +WORKDIR /usr/src/photo COPY . . RUN cmake . RUN make -j4 diff --git a/samples/photo/src/main.cpp b/samples/photo/src/main.cpp index 1b81f27..03ca437 100644 --- a/samples/photo/src/main.cpp +++ b/samples/photo/src/main.cpp @@ -1,9 +1,6 @@ -// -// Created by Oleg Morozenkov on 25.01.17. -// - -#include <signal.h> -#include <stdio.h> +#include <csignal> +#include <cstdio> +#include <cstdlib> #include <exception> #include <tgbot/tgbot.h> @@ -14,32 +11,35 @@ using namespace TgBot; bool sigintGot = false; int main() { - const string photoFilePath = "example.jpg"; - const string photoMimeType = "image/jpeg"; - - Bot bot("PLACE YOUR TOKEN HERE"); - bot.getEvents().onCommand("start", [&bot](Message::Ptr message) { - bot.getApi().sendMessage(message->chat->id, "Hi!"); - }); - bot.getEvents().onCommand("photo", [&bot, &photoFilePath, &photoMimeType](Message::Ptr message) { - bot.getApi().sendPhoto(message->chat->id, InputFile::fromFile(photoFilePath, photoMimeType)); - }); - - signal(SIGINT, [](int s) { - printf("SIGINT got"); - sigintGot = true; - }); - try { - printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); - - TgLongPoll longPoll(bot); - while (!sigintGot) { - printf("Long poll started\n"); - longPoll.start(); - } - } catch (exception& e) { - printf("error: %s\n", e.what()); - } - - return 0; + string token(getenv("TOKEN")); + printf("Token: %s\n", token.c_str()); + + const string photoFilePath = "example.jpg"; + const string photoMimeType = "image/jpeg"; + + Bot bot(token); + bot.getEvents().onCommand("start", [&bot](Message::Ptr message) { + bot.getApi().sendMessage(message->chat->id, "Hi!"); + }); + bot.getEvents().onCommand("photo", [&bot, &photoFilePath, &photoMimeType](Message::Ptr message) { + bot.getApi().sendPhoto(message->chat->id, InputFile::fromFile(photoFilePath, photoMimeType)); + }); + + signal(SIGINT, [](int s) { + printf("SIGINT got\n"); + sigintGot = true; + }); + try { + printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + + TgLongPoll longPoll(bot); + while (!sigintGot) { + printf("Long poll started\n"); + longPoll.start(); + } + } catch (exception& e) { + printf("error: %s\n", e.what()); + } + + return 0; } |