60 lines
1.5 KiB
MQL5
60 lines
1.5 KiB
MQL5
#include "TelegramResponse.mq5"
|
|
|
|
#property library
|
|
|
|
class TelegramApi
|
|
{
|
|
protected:
|
|
string baseUrl;
|
|
string token;
|
|
public:
|
|
/**
|
|
@var string _token - your bot's token
|
|
|
|
token should be url encoded twice
|
|
**/
|
|
void TelegramApi(string _token): token(_token) {
|
|
baseUrl = "https://api.telegram.org/bot";
|
|
};
|
|
string getBaseUrl();
|
|
TelegramResponse* sendMessage(string chatId, string text);
|
|
};
|
|
|
|
string TelegramApi::getBaseUrl(void)
|
|
{
|
|
return StringFormat("%s%s", baseUrl, token);
|
|
}
|
|
|
|
TelegramResponse* TelegramApi::sendMessage(string chatId, string text)
|
|
{
|
|
string url = StringFormat("%s/sendMessage?chat_id=%s&text=%s", getBaseUrl(), chatId, text);
|
|
string headers="";
|
|
string cookie=NULL,responseHeaders;
|
|
|
|
char post[], result[];
|
|
|
|
int responseCode = WebRequest(
|
|
"POST",
|
|
url,
|
|
headers,
|
|
120,
|
|
post,
|
|
result,
|
|
responseHeaders
|
|
);
|
|
|
|
TelegramResponse* response = new TelegramResponse();
|
|
|
|
if (responseCode == -1) {
|
|
response.setLastError(StringFormat("WebRequest error. Code %d.", GetLastError()));
|
|
} else if (responseCode == 200) {
|
|
response.setHeaders(headers);
|
|
response.setResult(result);
|
|
} else {
|
|
response.setLastError(StringFormat("Code %d returned. Code %d", responseCode, GetLastError()));
|
|
response.setHeaders(headers);
|
|
response.setResult(result);
|
|
}
|
|
|
|
return response;
|
|
}
|