Skip to content

Commit e96d3a3

Browse files
committed
Merge remote-tracking branch 'llnulldisk/master' into merge-228
2 parents 3013660 + 7abb250 commit e96d3a3

39 files changed

Lines changed: 3426 additions & 2787 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CMakeCache.txt
22
CMakeFiles
3+
CMakeSettings.json
34
Makefile
45
*.cmake
56
install_manifest.txt
@@ -16,10 +17,10 @@ docs/
1617
cmake-build-*
1718

1819
# Visual Studio
19-
/.vs/
20+
.vs/
2021
.vscode/
21-
/build/
22-
/out/
22+
build/
23+
out/
2324

2425
# MacOS dependecies
2526
.DS_Store

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 2.8.12)
1+
cmake_minimum_required(VERSION 3.10.2)
22
project(TgBot)
33

44
# options
@@ -51,7 +51,7 @@ find_package(OpenSSL REQUIRED)
5151
include_directories(${OPENSSL_INCLUDE_DIR})
5252

5353
## curl
54-
find_package(CURL 7.56.0)
54+
find_package(CURL 7.58.0)
5555
if (CURL_FOUND)
5656
include_directories(${CURL_INCLUDE_DIRS})
5757
add_definitions(-DHAVE_CURL)
@@ -60,9 +60,9 @@ endif()
6060
## boost
6161
set(Boost_USE_MULTITHREADED ON)
6262
if (ENABLE_TESTS)
63-
find_package(Boost 1.59.0 COMPONENTS system unit_test_framework REQUIRED)
63+
find_package(Boost 1.65.1 COMPONENTS system unit_test_framework REQUIRED)
6464
else()
65-
find_package(Boost 1.59.0 COMPONENTS system REQUIRED)
65+
find_package(Boost 1.65.1 COMPONENTS system REQUIRED)
6666
endif()
6767
include_directories(${Boost_INCLUDE_DIR})
6868
link_directories(${Boost_LIBRARY_DIR_RELEASE})

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM debian:stretch
1+
FROM debian:buster
22
MAINTAINER Oleg Morozenkov <m@oleg.rocks>
33

44
RUN apt-get -qq update && \

Dockerfile_test

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ WORKDIR /usr/src/tgbot-cpp/samples/echobot-curl-client
4040
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
4141
cmake . && make -j$(nproc)
4242

43+
WORKDIR /usr/src/tgbot-cpp/samples/echobot-setmycommands
44+
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
45+
cmake . && make -j$(nproc)
46+
47+
WORKDIR /usr/src/tgbot-cpp
48+
RUN rm -rf samples/echobot-submodule/tgbot-cpp/
49+
COPY include samples/echobot-submodule/tgbot-cpp/include
50+
COPY src samples/echobot-submodule/tgbot-cpp/src
51+
COPY CMakeLists.txt samples/echobot-submodule/tgbot-cpp/
52+
53+
WORKDIR /usr/src/tgbot-cpp/samples/echobot-submodule
54+
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
55+
cmake . && make -j$(nproc)
56+
4357
WORKDIR /usr/src/tgbot-cpp/samples/echobot-webhook-server
4458
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
4559
cmake . && make -j$(nproc)
@@ -56,7 +70,10 @@ WORKDIR /usr/src/tgbot-cpp/samples/received-text-processing
5670
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
5771
cmake . && make -j$(nproc)
5872

73+
WORKDIR /usr/src/tgbot-cpp/samples/reply-keyboard
74+
RUN rm -rf CMakeCache.txt CMakeFiles/ && \
75+
cmake . && make -j$(nproc)
76+
5977
WORKDIR /usr/src/tgbot-cpp
6078
ENV CTEST_OUTPUT_ON_FAILURE=1
6179
CMD make test
62-

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,57 @@ You can treat this repository as a submodule of your project, for example, see [
7373

7474
You can use Docker to build and run your bot. Set the base image of your's Dockerfile to [reo7sp/tgbot-cpp](https://hub.docker.com/r/reo7sp/tgbot-cpp/).
7575

76+
## Installation Windows
77+
78+
### Download vcpkg and tgbot-cpp
79+
80+
Taken from [Vcpkg - Quick Start: Windows](https://github.com/Microsoft/vcpkg/#quick-start-windows):
81+
82+
Prerequisites:
83+
- Windows 7 or newer
84+
- Git
85+
- Visual Studio 2015 Update 3 or greater with the English language pack
86+
87+
First, download and bootstrap vcpkg itself; it can be installed anywhere,
88+
but generally we recommend using vcpkg as a submodule for CMake projects,
89+
and installing it globally for Visual Studio projects.
90+
We recommend somewhere like `C:\src\vcpkg` or `C:\dev\vcpkg`,
91+
since otherwise you may run into path issues for some port build systems.
92+
93+
```cmd
94+
> git clone https://github.com/microsoft/vcpkg
95+
> .\vcpkg\bootstrap-vcpkg.bat
96+
```
97+
98+
In order to use vcpkg with Visual Studio,
99+
run the following command (may require administrator elevation):
100+
101+
```cmd
102+
> .\vcpkg\vcpkg integrate install
103+
```
104+
105+
To install the libraries for Windows x64, run:
106+
107+
```cmd
108+
> .\vcpkg\vcpkg install tgbot-cpp:x64-windows
109+
```
110+
111+
To install for Windows x86, run:
112+
113+
```cmd
114+
> .\vcpkg\vcpkg install tgbot-cpp
115+
```
116+
117+
The library will now be installed and Visual Studio should be able to find the vcpkg installation.
118+
119+
### Setup project with CMakeLists
120+
121+
Use the [example CMakeLists.txt](samples/echobot/CMakeLists.txt) with changes:
122+
123+
Remove `/usr/local/include`
124+
125+
Change `/usr/local/lib/libTgBot.a` to `C:/src/vcpkg/installed/x64-windows/lib/TgBot.lib` or something simmilar according to your own installation path.
126+
76127

77128
## Bot compilation
78129

include/tgbot/Api.h

Lines changed: 463 additions & 271 deletions
Large diffs are not rendered by default.

include/tgbot/EventBroadcaster.h

Lines changed: 156 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
#include "tgbot/types/InlineQuery.h"
77
#include "tgbot/types/ChosenInlineResult.h"
88
#include "tgbot/types/CallbackQuery.h"
9+
#include "tgbot/types/ShippingQuery.h"
10+
#include "tgbot/types/PreCheckoutQuery.h"
11+
#include "tgbot/types/Poll.h"
12+
#include "tgbot/types/PollAnswer.h"
13+
#include "tgbot/types/ChatMemberUpdated.h"
14+
#include "tgbot/types/ChatJoinRequest.h"
915

16+
#include <functional>
1017
#include <initializer_list>
1118
#include <string>
12-
#include <functional>
13-
#include <vector>
1419
#include <unordered_map>
20+
#include <vector>
1521

1622
namespace TgBot {
1723

@@ -31,9 +37,15 @@ friend EventHandler;
3137
typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener;
3238
typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener;
3339
typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener;
40+
typedef std::function<void (const ShippingQuery::Ptr)> ShippingQueryListener;
41+
typedef std::function<void (const PreCheckoutQuery::Ptr)> PreCheckoutQueryListener;
42+
typedef std::function<void (const Poll::Ptr)> PollListener;
43+
typedef std::function<void (const PollAnswer::Ptr)> PollAnswerListener;
44+
typedef std::function<void (const ChatMemberUpdated::Ptr)> ChatMemberUpdatedListener;
45+
typedef std::function<void (const ChatJoinRequest::Ptr)> ChatJoinRequestListener;
3446

3547
/**
36-
* @brief Registers listener which receives all messages which the bot can ever receive.
48+
* @brief Registers listener which receives new incoming message of any kind - text, photo, sticker, etc.
3749
* @param listener Listener.
3850
*/
3951
inline void onAnyMessage(const MessageListener& listener) {
@@ -43,21 +55,30 @@ friend EventHandler;
4355
/**
4456
* @brief Registers listener which receives all messages with commands (messages with leading '/' char).
4557
* @param commandName Command name which listener can handle.
46-
* @param listener Listener.
58+
* @param listener Listener. Pass nullptr to remove listener of command
4759
*/
4860
inline void onCommand(const std::string& commandName, const MessageListener& listener) {
49-
_onCommandListeners[commandName] = listener;
61+
if (listener) {
62+
_onCommandListeners[commandName] = listener;
63+
} else {
64+
_onCommandListeners.erase(commandName);
65+
}
5066
}
5167

5268
/**
5369
* @brief Registers listener which receives all messages with commands (messages with leading '/' char).
5470
* @param commandsList Commands names which listener can handle.
55-
* @param listener Listener.
71+
* @param listener Listener. Pass nullptr to remove listener of commands
5672
*/
5773
inline void onCommand(const std::initializer_list<std::string>& commandsList, const MessageListener& listener) {
58-
for (const auto& command : commandsList)
59-
{
60-
_onCommandListeners[command] = listener;
74+
if (listener) {
75+
for (const auto& command : commandsList) {
76+
_onCommandListeners[command] = listener;
77+
}
78+
} else {
79+
for (const auto& command : commandsList) {
80+
_onCommandListeners.erase(command);
81+
}
6182
}
6283
}
6384

@@ -78,25 +99,109 @@ friend EventHandler;
7899
}
79100

80101
/**
81-
* @brief Registers listener which receives all the inline query.
102+
* @brief Registers listener which receives new versions of a message that is known to the bot and was edited
103+
* @param listener Listener.
104+
*/
105+
inline void onEditedMessage(const MessageListener& listener) {
106+
_onEditedMessageListeners.push_back(listener);
107+
}
108+
109+
/**
110+
* @brief Registers listener which receives new incoming inline queries
82111
* @param listener Listener.
83112
*/
84113
inline void onInlineQuery(const InlineQueryListener& listener) {
85114
_onInlineQueryListeners.push_back(listener);
86115
}
87116

88117
/**
89-
* @brief Registers listener which receives all the chosen inline result.
118+
* @brief Registers listener which receives the results of an inline query that was chosen by a user and sent to their chat partner.
119+
* Please see https://core.telegram.org/bots/inline#collecting-feedback for details on how to enable these updates for your bot.
120+
*
90121
* @param listener Listener.
91122
*/
92123
inline void onChosenInlineResult(const ChosenInlineResultListener& listener){
93124
_onChosenInlineResultListeners.push_back(listener);
94125
}
95126

127+
/**
128+
* @brief Registers listener which receives new incoming callback queries
129+
* @param listener Listener.
130+
*/
96131
inline void onCallbackQuery(const CallbackQueryListener& listener){
97132
_onCallbackQueryListeners.push_back(listener);
98133
}
99134

135+
/**
136+
* @brief Registers listener which receives new incoming shipping queries.
137+
* Only for invoices with flexible price
138+
*
139+
* @param listener Listener.
140+
*/
141+
inline void onShippingQuery(const ShippingQueryListener& listener){
142+
_onShippingQueryListeners.push_back(listener);
143+
}
144+
145+
/**
146+
* @brief Registers listener which receives new incoming pre-checkout queries.
147+
* Contains full information about checkout
148+
*
149+
* @param listener Listener.
150+
*/
151+
inline void onPreCheckoutQuery(const PreCheckoutQueryListener& listener){
152+
_onPreCheckoutQueryListeners.push_back(listener);
153+
}
154+
155+
/**
156+
* @brief Registers listener which receives new poll states.
157+
* Bots receive only updates about stopped polls and polls, which are sent by the bot
158+
*
159+
* @param listener Listener.
160+
*/
161+
inline void onPoll(const PollListener& listener){
162+
_onPollListeners.push_back(listener);
163+
}
164+
165+
/**
166+
* @brief Registers listener which receives an answer if a user changed their answer in a non-anonymous poll.
167+
* Bots receive new votes only in polls that were sent by the bot itself.
168+
*
169+
* @param listener Listener.
170+
*/
171+
inline void onPollAnswer(const PollAnswerListener& listener){
172+
_onPollAnswerListeners.push_back(listener);
173+
}
174+
175+
/**
176+
* @brief Registers listener which receives the bot's chat member status if it was updated in a chat.
177+
* For private chats, this update is received only when the bot is blocked or unblocked by the user.
178+
*
179+
* @param listener Listener.
180+
*/
181+
inline void onMyChatMember(const ChatMemberUpdatedListener& listener){
182+
_onMyChatMemberListeners.push_back(listener);
183+
}
184+
185+
/**
186+
* @brief Registers listener which receives a status if a chat member's status was updated in a chat.
187+
* The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowedUpdates to receive these updates.
188+
*
189+
* @param listener Listener.
190+
*/
191+
inline void onChatMember(const ChatMemberUpdatedListener& listener){
192+
_onChatMemberListeners.push_back(listener);
193+
}
194+
195+
/**
196+
* @brief Registers listener which receives requests to join the chat.
197+
* The bot must have the canInviteUsers administrator right in the chat to receive these updates.
198+
*
199+
* @param listener Listener.
200+
*/
201+
inline void onChatJoinRequest(const ChatJoinRequestListener& listener){
202+
_onChatJoinRequestListeners.push_back(listener);
203+
}
204+
100205
private:
101206
template<typename ListenerType, typename ObjectType>
102207
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
@@ -129,6 +234,10 @@ friend EventHandler;
129234
broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
130235
}
131236

237+
inline void broadcastEditedMessage(const Message::Ptr& message) const {
238+
broadcast<MessageListener, Message::Ptr>(_onEditedMessageListeners, message);
239+
}
240+
132241
inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const {
133242
broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
134243
}
@@ -141,13 +250,49 @@ friend EventHandler;
141250
broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
142251
}
143252

253+
inline void broadcastShippingQuery(const ShippingQuery::Ptr& result) const {
254+
broadcast<ShippingQueryListener, ShippingQuery::Ptr>(_onShippingQueryListeners, result);
255+
}
256+
257+
inline void broadcastPreCheckoutQuery(const PreCheckoutQuery::Ptr& result) const {
258+
broadcast<PreCheckoutQueryListener, PreCheckoutQuery::Ptr>(_onPreCheckoutQueryListeners, result);
259+
}
260+
261+
inline void broadcastPoll(const Poll::Ptr& result) const {
262+
broadcast<PollListener, Poll::Ptr>(_onPollListeners, result);
263+
}
264+
265+
inline void broadcastPollAnswer(const PollAnswer::Ptr& result) const {
266+
broadcast<PollAnswerListener, PollAnswer::Ptr>(_onPollAnswerListeners, result);
267+
}
268+
269+
inline void broadcastMyChatMember(const ChatMemberUpdated::Ptr& result) const {
270+
broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onMyChatMemberListeners, result);
271+
}
272+
273+
inline void broadcastChatMember(const ChatMemberUpdated::Ptr& result) const {
274+
broadcast<ChatMemberUpdatedListener, ChatMemberUpdated::Ptr>(_onChatMemberListeners, result);
275+
}
276+
277+
inline void broadcastChatJoinRequest(const ChatJoinRequest::Ptr& result) const {
278+
broadcast<ChatJoinRequestListener, ChatJoinRequest::Ptr>(_onChatJoinRequestListeners, result);
279+
}
280+
144281
std::vector<MessageListener> _onAnyMessageListeners;
145282
std::unordered_map<std::string, MessageListener> _onCommandListeners;
146283
std::vector<MessageListener> _onUnknownCommandListeners;
147284
std::vector<MessageListener> _onNonCommandMessageListeners;
285+
std::vector<MessageListener> _onEditedMessageListeners;
148286
std::vector<InlineQueryListener> _onInlineQueryListeners;
149287
std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
150288
std::vector<CallbackQueryListener> _onCallbackQueryListeners;
289+
std::vector<ShippingQueryListener> _onShippingQueryListeners;
290+
std::vector<PreCheckoutQueryListener> _onPreCheckoutQueryListeners;
291+
std::vector<PollListener> _onPollListeners;
292+
std::vector<PollAnswerListener> _onPollAnswerListeners;
293+
std::vector<ChatMemberUpdatedListener> _onMyChatMemberListeners;
294+
std::vector<ChatMemberUpdatedListener> _onChatMemberListeners;
295+
std::vector<ChatJoinRequestListener> _onChatJoinRequestListeners;
151296
};
152297

153298
}

0 commit comments

Comments
 (0)