Skip to content

Commit ab7ce1f

Browse files
authored
Merge pull request #255 from reo7sp/Ctulh-master
Add file sample #254 with fixes
2 parents 8f49832 + 1aa8158 commit ab7ce1f

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

Dockerfile_test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ WORKDIR /usr/src/tgbot-cpp/samples/reply-keyboard
7474
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
7575
cmake . && make -j$(nproc)
7676

77+
WORKDIR /usr/src/tgbot-cpp/samples/receive-file
78+
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
79+
cmake . && make -j$(nproc)
80+
7781
WORKDIR /usr/src/tgbot-cpp
7882
ENV CTEST_OUTPUT_ON_FAILURE=1
7983
CMD make test
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
project(receive-file)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
7+
set(Boost_USE_MULTITHREADED ON)
8+
9+
find_package(Threads REQUIRED)
10+
find_package(OpenSSL REQUIRED)
11+
find_package(Boost COMPONENTS system REQUIRED)
12+
find_package(CURL)
13+
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
14+
if (CURL_FOUND)
15+
include_directories(${CURL_INCLUDE_DIRS})
16+
add_definitions(-DHAVE_CURL)
17+
endif()
18+
19+
add_executable(file src/main.cpp)
20+
21+
target_link_libraries(file /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})

samples/receive-file/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM reo7sp/tgbot-cpp
2+
MAINTAINER Oleg Morozenkov <m@oleg.rocks>
3+
4+
WORKDIR /usr/src/receive-file
5+
COPY . .
6+
RUN cmake .
7+
RUN make -j4
8+
CMD ./receive-file

samples/receive-file/src/main.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <csignal>
2+
#include <cstdio>
3+
#include <cstdlib>
4+
#include <exception>
5+
#include <string>
6+
7+
#include <tgbot/tgbot.h>
8+
9+
using namespace std;
10+
using namespace TgBot;
11+
12+
int main() {
13+
string token(getenv("TOKEN"));
14+
printf("Token: %s\n", token.c_str());
15+
16+
Bot bot(token);
17+
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
18+
bot.getApi().sendMessage(message->chat->id, "Hi!");
19+
});
20+
bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
21+
printf("User wrote %s\n", message->text.c_str());
22+
23+
File::Ptr file = bot.getApi().getFile(message->document->fileId);
24+
std::string fileContent = bot.getApi().downloadFile(file->filePath);
25+
26+
if (StringTools::startsWith(message->text, "/start")) {
27+
return;
28+
}
29+
bot.getApi().sendMessage(message->chat->id, "Your file content: " + fileContent);
30+
});
31+
32+
signal(SIGINT, [](int s) {
33+
printf("SIGINT got\n");
34+
exit(0);
35+
});
36+
37+
try {
38+
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
39+
bot.getApi().deleteWebhook();
40+
41+
TgLongPoll longPoll(bot);
42+
while (true) {
43+
printf("Long poll started\n");
44+
longPoll.start();
45+
}
46+
} catch (exception& e) {
47+
printf("error: %s\n", e.what());
48+
}
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)