7 #include "cppgram/basic_bot.hpp" 8 #include "cppgram/commands/command.hpp" 9 #include "cppgram/exception.hpp" 10 #include "cppgram/types/update.hpp" 13 BasicBot<T>::BasicBot(
const std::string &token, std::string name, T *obj_ptr )
14 : api_url(
"https://api.telegram.org/bot" + token +
"/" )
16 , logger_ptr( nullptr )
17 , command_handler( obj_ptr )
31 bot_name = b.bot_name;
33 logger_ptr = b.logger_ptr;
50 bot_name = b.bot_name;
52 logger_ptr = b.logger_ptr;
63 std::shared_ptr<spdlog::logger>
67 std::vector<spdlog::sink_ptr> sinks;
68 sinks.push_back( sink );
73 std::shared_ptr<spdlog::logger>
79 logger_ptr = std::make_shared<spdlog::logger>( bot_name, sinks.begin(), sinks.end() );
82 logger_ptr->flush_on( spdlog::level::err );
87 catch (
const spdlog::spdlog_ex &ex )
90 auto console = spdlog::get(
"error_console" );
91 console->error( ex.what() );
102 logger_ptr = new_logger;
109 command_handler.init( dynamic_cast<T *>(
this ) );
116 connection.SetUrl( api_url + method );
117 connection.SetParameters( params );
118 return connection.Get();
126 if ( response.error.code != cpr::ErrorCode::OK )
128 logger_ptr->error(
"HTTP Error:" + response.error.message );
132 if ( !reader.parse( response.text, val ) )
134 logger_ptr->error(
"JSON Parser: Error while parsing JSON document!" );
139 if ( response.status_code != 200 )
141 logger_ptr->error(
"Telegram Error: " + val[
"error_code"].asString() +
", Description: " 142 + val[
"description"].asString() );
152 const uint_fast32_t offset,
153 const uint_fast32_t limit,
154 const uint_fast32_t timeout )
157 if ( updates.size() != 0 )
164 cpr::Parameters{{
"timeout", std::to_string( timeout )},
165 {
"limit", std::to_string( limit )},
166 {
"offset", std::to_string( offset + 1 )}} );
168 Json::Value json_updates;
169 if ( !checkMethodError( response, json_updates ) || json_updates[
"result"].empty() )
175 for (
auto &json_single_update : json_updates[
"result"] )
178 updates.emplace_back( json_single_update );
186 std::experimental::optional<const cppgram::types::Message>
188 const std::string &text,
189 const std::string &reply_markup,
191 const bool disable_web_page_preview,
192 const bool disable_notification,
193 const int_fast32_t reply_to_message_id )
199 disable_web_page_preview,
200 disable_notification,
201 reply_to_message_id );
205 std::experimental::optional<const cppgram::types::Message>
207 const std::string &text,
208 const std::string &reply_markup,
210 const bool disable_web_page_preview,
211 const bool disable_notification,
212 const int_fast32_t reply_to_message_id )
214 std::string parse_mode_string =
"";
218 parse_mode_string =
"HTML";
222 parse_mode_string =
"Markdown";
228 {
"chat_id", chat_id},
230 {
"parse_mode", parse_mode_string},
231 {
"disable_web_page_preview", std::to_string( disable_web_page_preview )},
232 {
"disable_notification", std::to_string( disable_notification )},
233 {
"reply_to_message_id", std::to_string( reply_to_message_id )},
234 {
"reply_markup", reply_markup}} );
237 if ( !checkMethodError( response, valroot ) )
246 std::experimental::optional<const cppgram::types::Message>
248 const std::string &reply_markup,
250 const bool disable_web_page_preview,
251 const bool disable_notification,
252 const int_fast32_t reply_to_message_id )
258 disable_web_page_preview,
259 disable_notification,
260 reply_to_message_id );
268 const std::string &
url )
271 cpr::Parameters{{
"callback_query", callback_query_id},
273 {
"show_alert", std::to_string( show_alert )},
274 {
"cache_time", std::to_string( cache_time )},
278 if ( !checkMethodError( response, valroot ) )
287 std::experimental::optional<const cppgram::types::Message>
289 const uint_fast32_t message_id,
290 const std::string & text,
291 const std::string & reply_markup,
293 const bool disable_web_page_preview )
300 disable_web_page_preview );
304 std::experimental::optional<const cppgram::types::Message>
306 const uint_fast32_t message_id,
307 const std::string & text,
308 const std::string & reply_markup,
310 const bool disable_web_page_preview )
312 std::string parseMode =
"";
320 parseMode =
"Markdown";
324 cpr::Parameters{{
"chat_id", chat_id},
325 {
"message_id", std::to_string( message_id )},
327 {
"parse_mode", parseMode},
328 {
"disable_web_page_preview",
329 std::to_string( disable_web_page_preview )},
330 {
"reply_markup", reply_markup}} );
333 if ( !checkMethodError( response, valroot ) )
342 std::experimental::optional<const cppgram::types::Message>
344 const std::string & text,
345 const std::string & reply_markup,
347 const bool disable_web_page_preview )
354 disable_web_page_preview );
360 const std::string &text,
361 const std::string &reply_markup,
363 const bool disable_web_page_preview )
365 std::string parseMode =
"";
373 parseMode =
"Markdown";
378 cpr::Parameters{{
"inline_message_id", inline_message_id},
380 {
"parse_mode", parseMode},
381 {
"disable_web_page_preview", disable_web_page_preview},
382 {
"reply_markup", reply_markup}} );
385 if ( !checkMethodError( response, valroot ) )
390 return valroot[
"result"].asBool();
394 std::experimental::optional<const cppgram::types::Message>
396 const uint_fast32_t message_id,
397 const std::string & caption,
398 const std::string & reply_markup )
400 return editMessageCaption( std::to_string( chat_id ), message_id, caption, reply_markup );
404 std::experimental::optional<const cppgram::types::Message>
406 const uint_fast32_t message_id,
407 const std::string & caption,
408 const std::string & reply_markup )
411 cpr::Parameters{{
"chat_id", chat_id},
412 {
"message_id", std::to_string( message_id )},
413 {
"caption", caption},
414 {
"reply_markup", reply_markup}} );
417 if ( !checkMethodError( response, valroot ) )
426 std::experimental::optional<const cppgram::types::Message>
428 const std::string & caption,
429 const std::string & reply_markup )
431 return editMessageCaption( std::to_string( chat_id ), message_id, caption, reply_markup );
437 const std::string &caption,
438 const std::string &reply_markup )
441 cpr::Parameters{{
"inline_message_id", inline_message_id},
442 {
"caption", caption},
443 {
"reply_markup", reply_markup}} );
446 if ( !checkMethodError( response, valroot ) )
451 return valroot[
"result"].asBool();
455 std::experimental::optional<const cppgram::types::Message>
457 const uint_fast32_t message_id,
458 const std::string & reply_markup )
464 std::experimental::optional<const cppgram::types::Message>
466 const uint_fast32_t message_id,
467 const std::string & reply_markup )
470 cpr::Parameters{{
"chat_id", chat_id},
471 {
"message_id", std::to_string( message_id )},
472 {
"reply_markup", reply_markup}} );
475 if ( !checkMethodError( response, valroot ) )
484 std::experimental::optional<const cppgram::types::Message>
486 const std::string & reply_markup )
494 const std::string &reply_markup )
497 cpr::Parameters{{
"inline_message_id", inline_message_id},
498 {
"reply_markup", reply_markup}} );
501 if ( !checkMethodError( response, valroot ) )
506 return valroot[
"result"].asBool();
512 const uint_fast16_t cache_time,
513 const bool is_personal,
514 const std::string & next_offset,
515 const std::string & switch_pm_text,
516 const std::string & switch_pm_paramter )
518 Json::FastWriter writer;
521 cpr::Parameters{{
"inline_query_id", inline_query_id},
522 {
"results", writer.write( results )},
523 {
"cache_time", std::to_string( cache_time )},
524 {
"is_personal", is_personal},
525 {
"next_offset", next_offset},
526 {
"switch_pm_text", switch_pm_paramter},
527 {
"switch_pm_parameter", switch_pm_paramter}} );
530 if ( !checkMethodError( response, valroot ) )
535 return valroot[
"result"].asBool();
542 if ( command_handler.processCommands( update ) )
547 T &bot =
dynamic_cast<T &
>( *this );
548 switch ( update.type )
550 case EUpdate::eMessage:
554 chat_id = update.message->chat.id;
559 case EUpdate::eCallbackQuery:
561 chat_id = update.callback_query->message->chat.id;
562 callback_query_id = update.callback_query->id;
564 callback_query_id =
"";
567 case EUpdate::eEditedMessage:
571 chat_id = update.message->chat.id;
576 case EUpdate::eInlineQuery:
578 chat_id = update.inline_query->from.id;
579 inline_query_id = update.inline_query->id;
581 inline_query_id =
"";
584 case EUpdate::eChosenInlineResult:
586 chat_id = update.chosen_inline_result->from.id;
590 case EUpdate::eChannelPost:
592 chat_id = update.message->chat.id;
596 case EUpdate::eEditedChannelPost:
598 chat_id = update.message->chat.id;
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
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's logger by passing a sink.
Definition: basic_bot_impl.hpp:64
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
const cpr::Response executeRequest(const std::string &method, const cpr::Parameters ¶ms)
Execute an API method by passing method name and parameters.
Definition: basic_bot_impl.hpp:114
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.
If jsoncpp can't parse the JSON document (critical).
Definition: exception.hpp:24
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
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
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) ...
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
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