Last active
January 8, 2024 05:30
-
-
Save ozlb/8e71cabfa69d1944ea8210308ab83c2a to your computer and use it in GitHub Desktop.
Telegram bot API via curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <time.h> | |
#include <ctype.h> | |
#include <string> | |
//https://github.com/DaveGamble/cJSON | |
#include "cJSON.h" | |
//You can find a step-by-step guide here https://core.telegram.org/bots/features#creating-a-new-bot | |
#define K_TELEGRAM_BOT_TOKEN_ID "4839574812:AAFD39kkdpWt3ywyRZergyOLMaJhac60qc" | |
#define APP_CURL "curl " | |
#define APP_CURL_PARAM_TIMEOUT "--connect-timeout 3 " | |
#define APP_CURL_PARAM_SILENT "-s " | |
#define APP_CURL_PARAM_HEADER_JSON "-H 'Content-Type: application/json' " | |
#define APP_CURL_PARAM_POST "-X POST " | |
#define APP_CURL_PARAM_URL "https://api.telegram.org/bot" K_TELEGRAM_BOT_TOKEN_ID | |
#define CMD_CURL APP_CURL APP_CURL_PARAM_TIMEOUT APP_CURL_PARAM_SILENT APP_CURL_PARAM_HEADER_JSON APP_CURL_PARAM_POST APP_CURL_PARAM_URL | |
#define AsString(x) ((std::string) x) | |
void telegram_api_getMyDescription() { | |
#define API_METHOD "getMyDescription" | |
std::string str = CMD_CURL "/" API_METHOD " "; | |
str += "-o " API_METHOD ".json"; | |
system("rm -f " API_METHOD ".json"); | |
system(str.c_str()); | |
#undef API_METHOD | |
} | |
void telegram_api_setMyDescription(const char *description) { | |
#define API_METHOD "setMyDescription" | |
std::string str = CMD_CURL "/" API_METHOD " "; | |
//{ "description":@description } | |
cJSON *jCurlPostParam = cJSON_CreateObject(); | |
cJSON_AddItemToObject(jCurlPostParam, "description", cJSON_CreateString(description)); | |
str += "-d '" + AsString(cJSON_PrintUnformatted(jCurlPostParam)) + "' "; | |
str += "-o " API_METHOD ".json"; | |
system("rm -f " API_METHOD ".json"); | |
system(str.c_str()); | |
cJSON_Delete(jCurlPostParam); | |
#undef API_METHOD | |
} | |
void telegram_api_getUpdates(unsigned int offset, unsigned int limit) { | |
#define API_METHOD "getUpdates" | |
std::string str = CMD_CURL "/" API_METHOD " "; | |
//{ "offset": @messageIdOffset, "limit" : @limit, "allowed_updates" : ["message", "callback_query"] } | |
cJSON *jCurlPostParam = cJSON_CreateObject(); | |
cJSON_AddItemToObject(jCurlPostParam, "offset", cJSON_CreateNumber(offset)); | |
cJSON_AddItemToObject(jCurlPostParam, "limit", cJSON_CreateNumber(limit)); | |
cJSON *jCurlPostParam_allowed_updates = cJSON_CreateArray(); | |
cJSON_AddItemToArray(jCurlPostParam_allowed_updates, cJSON_CreateString("message")); | |
cJSON_AddItemToArray(jCurlPostParam_allowed_updates, cJSON_CreateString("callback_query")); | |
cJSON_AddItemToObject(jCurlPostParam, "allowed_updates", jCurlPostParam_allowed_updates); | |
str += "-d '" + AsString(cJSON_PrintUnformatted(jCurlPostParam)) + "' "; | |
str += "-o " API_METHOD ".json"; | |
system("rm -f " API_METHOD ".json"); | |
system(str.c_str()); | |
cJSON_Delete(jCurlPostParam); | |
#undef API_METHOD | |
} | |
void telegram_api_sendMessage(unsigned int chat_id, const char *text, const char *reply_markup) { | |
#define API_METHOD "sendMessage" | |
std::string str = CMD_CURL "/" API_METHOD " "; | |
//{ "chat_id":@chat_id, "text":@text, "parse_mode" : "HTML", "reply_markup" : @reply_markup } | |
cJSON *jCurlPostParam = cJSON_CreateObject(); | |
cJSON_AddItemToObject(jCurlPostParam, "chat_id", cJSON_CreateNumber(chat_id)); | |
cJSON_AddItemToObject(jCurlPostParam, "text", cJSON_CreateString(text)); | |
cJSON_AddItemToObject(jCurlPostParam, "parse_mode", cJSON_CreateString("HTML")); | |
cJSON_AddItemToObject(jCurlPostParam, "reply_markup", cJSON_CreateString(reply_markup)); | |
str += "-d '" + AsString(cJSON_PrintUnformatted(jCurlPostParam)) + "' "; | |
str += "-o " API_METHOD ".json"; | |
system("rm -f " API_METHOD ".json"); | |
system(str.c_str()); | |
cJSON_Delete(jCurlPostParam); | |
#undef API_METHOD | |
} | |
void telegram_api_answerCallbackQuery(const char *callback_query_id, const char *text, int cache_time) { | |
#define API_METHOD "answerCallbackQuery" | |
std::string str = CMD_CURL "/" API_METHOD " "; | |
//{ "callback_query_id":@chat_id, "text":@text, "show_alert" : false, "cache_time" : @cache_time } | |
cJSON *jCurlPostParam = cJSON_CreateObject(); | |
cJSON_AddItemToObject(jCurlPostParam, "callback_query_id", cJSON_CreateString(callback_query_id)); | |
cJSON_AddItemToObject(jCurlPostParam, "text", cJSON_CreateString(text)); | |
cJSON_AddItemToObject(jCurlPostParam, "show_alert", cJSON_CreateBool(0)); | |
cJSON_AddItemToObject(jCurlPostParam, "cache_time", cJSON_CreateNumber(cache_time)); | |
str += "-d '" + AsString(cJSON_PrintUnformatted(jCurlPostParam)) + "' "; | |
str += "-o " API_METHOD ".json"; | |
system("rm -f " API_METHOD ".json"); | |
system(str.c_str()); | |
cJSON_Delete(jCurlPostParam); | |
#undef API_METHOD | |
} | |
#undef APP_CURL | |
#undef APP_CURL_PARAM_TIMEOUT | |
#undef APP_CURL_PARAM_SILENT | |
#undef APP_CURL_PARAM_HEADER_JSON | |
#undef APP_CURL_PARAM_POST | |
#undef APP_CURL_PARAM_URL | |
#undef CMD_CURL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment