Skip to content

Commit 90dc11b

Browse files
committed
add file sample
1 parent 8f49832 commit 90dc11b

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

samples/file/CMakeLists.txt

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(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/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/photo
5+
COPY . .
6+
RUN cmake .
7+
RUN make -j4
8+
CMD ./photo

samples/file/src/main.cpp

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

0 commit comments

Comments
 (0)