Cppgram  1.0.0
Easy and modern C++14 Telegram Bot API wrapper
basic_bot.hpp
1 #ifndef CPPGRAM_BASIC_BOT_HPP
2 #define CPPGRAM_BASIC_BOT_HPP
3 
4 #include <string>
5 
6 #include <cpr/cpr.h>
7 #include <spdlog/spdlog.h>
8 
9 #include "command_handler.hpp"
10 #include "exception.hpp"
11 #include "keyboard.hpp"
12 #include "types/update.hpp"
13 
193 namespace cppgram
194 {
199 template <class T> class BasicBot
200 {
201  template <typename V> friend class Polling;
202  template <class Z> friend class commands::Command;
203 
204  public:
213  BasicBot( const std::string &token, std::string name = "Bot", T *obj_ptr = nullptr );
214 
219  BasicBot( const BasicBot &b, T *base_ptr );
220 
221  virtual ~BasicBot() {}
222 
228  BasicBot operator=( const BasicBot &b );
229 
234  inline void setChatId( uint_fast32_t chat_id ) { this->chat_id = chat_id; }
235 
240  inline void setChatId( int_fast64_t chat_id ) { this->chat_id = chat_id; }
241 
246  inline CommandHandler<T> &commands() { return command_handler; }
247 
252  inline Keyboard &keyboard() { return keyboard_obj; }
253 
258  inline std::shared_ptr<spdlog::logger> logger() { return logger_ptr; }
259 
266  std::shared_ptr<spdlog::logger> setLogger( spdlog::sink_ptr sink );
267 
275  inline std::shared_ptr<spdlog::logger> setLogger( std::vector<spdlog::sink_ptr> &sinks );
276 
281  void setLogger( std::shared_ptr<spdlog::logger> new_logger );
282 
286  virtual void init();
287 
296  const cpr::Response executeRequest( const std::string &method, const cpr::Parameters &params );
297 
305  bool checkMethodError( const cpr::Response &response, Json::Value &val );
306 
328  bool getUpdates( std::vector<types::Update> &updates,
329  const uint_fast32_t offset = 0,
330  const uint_fast32_t limit = 100,
331  const uint_fast32_t timeout = 60 );
332 
348  std::experimental::optional<const class types::Message>
349  sendMessage( const int_fast64_t chat_id,
350  const std::string &text,
351  const std::string &reply_markup = "",
352  const EParseMode parse_mode = EParseMode::HTML,
353  const bool disable_web_page_preview = true,
354  const bool disable_notification = false,
355  const int_fast32_t reply_to_message_id = 0 );
356 
362  std::experimental::optional<const class types::Message>
363  sendMessage( const std::string &chat_id,
364  const std::string &text,
365  const std::string &reply_markup = "",
366  const EParseMode parse_mode = EParseMode::HTML,
367  const bool disable_web_page_preview = true,
368  const bool disable_notification = false,
369  const int_fast32_t reply_to_message_id = 0 );
370 
378  std::experimental::optional<const class types::Message>
379  sendMessage( const std::string &text,
380  const std::string &reply_markup = "",
381  const EParseMode parse_mode = EParseMode::HTML,
382  const bool disable_web_page_preview = true,
383  const bool disable_notification = false,
384  const int_fast32_t reply_to_message_id = 0 );
385 
401  bool answerCallbackQuery( const std::string &text,
402  bool show_alert = false,
403  uint32_t cache_time = 0,
404  const std::string &url = "" );
405 
419  std::experimental::optional<const class types::Message>
420  editMessageText( const int_fast64_t chat_id,
421  const uint_fast32_t message_id,
422  const std::string & text,
423  const std::string & reply_markup = "",
424  const EParseMode parse_mode = static_cast<EParseMode>( 1 ),
425  const bool disable_web_page_preview = true );
426 
433  std::experimental::optional<const class types::Message>
434  editMessageText( const std::string & chat_id,
435  const uint_fast32_t message_id,
436  const std::string & text,
437  const std::string & reply_markup = "",
438  const EParseMode parse_mode = static_cast<EParseMode>( 1 ),
439  const bool disable_web_page_preview = true );
440 
447  std::experimental::optional<const class types::Message>
448  editMessageText( const uint_fast32_t message_id,
449  const std::string & text,
450  const std::string & reply_markup = "",
451  const EParseMode parse_mode = static_cast<EParseMode>( 1 ),
452  const bool disable_web_page_preview = true );
466  bool editMessageText( const std::string &inline_message_id,
467  const std::string &text,
468  const std::string &reply_markup = "",
469  const EParseMode parse_mode = static_cast<EParseMode>( 1 ),
470  const bool disable_web_page_preview = true );
471 
481  std::experimental::optional<const class types::Message>
482  editMessageCaption( const int_fast64_t chat_id,
483  const uint_fast32_t message_id,
484  const std::string & caption = "",
485  const std::string & reply_markup = "" );
486 
493  std::experimental::optional<const class types::Message>
494  editMessageCaption( const std::string & chat_id,
495  const uint_fast32_t message_id,
496  const std::string & caption = "",
497  const std::string & reply_markup = "" );
498 
504  std::experimental::optional<const class types::Message>
505  editMessageCaption( const uint_fast32_t message_id,
506  const std::string & caption = "",
507  const std::string & reply_markup = "" );
508 
518  bool editMessageCaption( const std::string &inline_message_id,
519  const std::string &caption,
520  const std::string &reply_markup = "" );
521 
531  std::experimental::optional<const class types::Message>
532  editMessageReplyMarkup( const int_fast64_t chat_id,
533  const uint_fast32_t message_id,
534  const std::string & reply_markup = "" );
535 
543  std::experimental::optional<const class types::Message>
544  editMessageReplyMarkup( const std::string & chat_id,
545  const uint_fast32_t message_id,
546  const std::string & reply_markup = "" );
547 
554  std::experimental::optional<const class types::Message>
555  editMessageReplyMarkup( const uint_fast32_t message_id, const std::string &reply_markup = "" );
556 
565  bool editMessageReplyMarkup( const std::string &inline_message_id,
566  const std::string &reply_markup = "" );
567 
588  bool answerInlineQuery( const Json::Value & results,
589  const uint_fast16_t cache_time = 300,
590  const bool is_personal = false,
591  const std::string & next_offset = "",
592  const std::string & switch_pm_text = "",
593  const std::string & switch_pm_paramter = "" );
594 
606  std::function<void( T &, const types::Message & )> processMessage = nullptr;
607 
611  void ( *processEditedMessage )( T &, const types::Message & ) = nullptr;
612 
616  void ( *processChannelPost )( T &, const types::Message & ) = nullptr;
617 
621  void ( *processEditedChannelPost )( T &, const types::Message & ) = nullptr;
622 
626  void ( *processInlineQuery )( T &, const types::InlineQuery & ) = nullptr;
627 
631  void ( *processChosenInlineResult )( T &, const types::ChosenInlineResult & ) = nullptr;
632 
636  void ( *processCallbackQuery )( T &, const types::CallbackQuery & ) = nullptr;
637 
640  private:
646  void processUpdate( const types::Update &update );
647 
649  int_fast64_t chat_id;
650 
652  std::string callback_query_id;
653 
655  std::string inline_query_id;
656 
658  Json::Reader reader;
659 
661  cpr::Session connection;
662 
664  std::shared_ptr<spdlog::logger> logger_ptr;
665 
667  std::string api_url,
668 
670  bot_name;
671 
673  CommandHandler<T> command_handler;
674 
676  Keyboard keyboard_obj;
677 };
678 }
679 
680 #include "basic_bot_impl.hpp"
681 
682 #endif
void(* processInlineQuery)(T &, const types::InlineQuery &)
Pointer to the function that will be called on each inline query.
Definition: basic_bot.hpp:626
void(* processEditedMessage)(T &, const types::Message &)
Pointer to the function that will be called on each edited message by the user.
Definition: basic_bot.hpp:611
bool getUpdates(std::vector< types::Update > &updates, const uint_fast32_t offset=0, const uint_fast32_t limit=100, const uint_fast32_t timeout=60)
Receive incoming updates using polling (short or long polling based on timeout)
Definition: basic_bot_impl.hpp:151
An helper class to create inline keyboard.
Definition: keyboard.hpp:33
A message send by user.
Definition: message.hpp:36
BasicBot(const std::string &token, std::string name="Bot", T *obj_ptr=nullptr)
Constuctor.
std::shared_ptr< spdlog::logger > setLogger(spdlog::sink_ptr sink)
Set the bot&#39;s logger by passing a sink.
Definition: basic_bot_impl.hpp:64
Data from an inline keyboard button pressed.
Definition: callback_query.hpp:21
Handle bot commands.
Definition: command_handler.hpp:26
void(* processEditedChannelPost)(T &, const types::Message &)
Pointer to the function that will be called on each message modified in a channel.
Definition: basic_bot.hpp:621
std::function< void(T &, const types::Message &)> processMessage
Pointer to the function that will be called on each message sent by the user.
Definition: basic_bot.hpp:606
virtual void init()
Init bot object.
Definition: basic_bot_impl.hpp:107
CommandHandler< T > & commands()
Get command handler object.
Definition: basic_bot.hpp:246
const cpr::Response executeRequest(const std::string &method, const cpr::Parameters &params)
Execute an API method by passing method name and parameters.
Definition: basic_bot_impl.hpp:114
Incoming inline query.
Definition: inline_query.hpp:22
void setChatId(int_fast64_t chat_id)
Set the chat id of the bot.
Definition: basic_bot.hpp:240
std::experimental::optional< const class types::Message > editMessageCaption(const int_fast64_t chat_id, const uint_fast32_t message_id, const std::string &caption="", const std::string &reply_markup="")
Edit captions of messages sent by the bot.
std::experimental::optional< const class types::Message > editMessageText(const int_fast64_t chat_id, const uint_fast32_t message_id, const std::string &text, const std::string &reply_markup="", const EParseMode parse_mode=static_cast< EParseMode >(1), const bool disable_web_page_preview=true)
Edit text (and reply markup) of a message sent by the bot.
void(* processCallbackQuery)(T &, const types::CallbackQuery &)
Pointer to the funciton that will be called on each callback query.
Definition: basic_bot.hpp:636
contains api methods, update handlers and listener.
Definition: basic_bot.hpp:199
EParseMode
Formattation options.
Definition: enums.hpp:66
Abstract class for bot commands.
Definition: command_handler.hpp:15
Keyboard & keyboard()
Get the keyboard handler.
Definition: basic_bot.hpp:252
bool answerInlineQuery(const Json::Value &results, const uint_fast16_t cache_time=300, const bool is_personal=false, const std::string &next_offset="", const std::string &switch_pm_text="", const std::string &switch_pm_paramter="")
Answer an inline query. (https://core.telegram.org/bots/api#answerinlinequery)
Definition: basic_bot_impl.hpp:511
bool answerCallbackQuery(const std::string &text, bool show_alert=false, uint32_t cache_time=0, const std::string &url="")
Use this method to send answers to callback queries sent from inline keyboards. The answer will be di...
Definition: basic_bot_impl.hpp:265
Definition: enums.hpp:72
std::experimental::optional< const class types::Message > editMessageReplyMarkup(const int_fast64_t chat_id, const uint_fast32_t message_id, const std::string &reply_markup="")
Edit only the reply markup of a message sent by the the bot.
std::experimental::optional< const class types::Message > sendMessage(const int_fast64_t chat_id, const std::string &text, const std::string &reply_markup="", const EParseMode parse_mode=EParseMode::HTML, const bool disable_web_page_preview=true, const bool disable_notification=false, const int_fast32_t reply_to_message_id=0)
Send a message to a specified chat. (https://core.telegram.org/bots/api#sendmessage) ...
main namespace for Cppgram
void(* processChannelPost)(T &, const types::Message &)
Pointer to the function that will be called on each message received in a channel.
Definition: basic_bot.hpp:616
Definition: enums.hpp:40
Handle long (or short) polling for a bot.
Definition: polling.hpp:12
void setChatId(uint_fast32_t chat_id)
Set the chat id of the bot.
Definition: basic_bot.hpp:234
A inline query choosed by the user.
Definition: chosen_inline_result.hpp:22
std::shared_ptr< spdlog::logger > logger()
Returns current bot&#39;s logger.
Definition: basic_bot.hpp:258
void(* processChosenInlineResult)(T &, const types::ChosenInlineResult &)
Pointer to the function that will be called on each inline query choosed by the user.
Definition: basic_bot.hpp:631