Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

HEADERS += \
$$PWD/src/telegrambot.h \
$$PWD/modules/httpserver/httpserver.h \
$$PWD/vendor/qdelegate/include/qdelegate.h

INCLUDEPATH += $$PWD/src/
INCLUDEPATH += $$PWD/modules/sslserver/
INCLUDEPATH += $$PWD/modules/httpserver/
INCLUDEPATH += $$PWD/vendor/qdelegate/include/

7 changes: 4 additions & 3 deletions modules/httpserver/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ void HttpServer::handleNewData()

// build response
QByteArray responseContent;
responseContent += (response->version.isEmpty() ? "HTTP/1.1" : response->version) + " "; // Version
responseContent += QByteArray::number((qint32)response->status) + " " + response->StatusNames.value((qint32)response->status, "") + "\r\n"; // Status
responseContent += (response->version.isEmpty() ? "HTTP/1.1" : response->version.toUtf8()) + " "; // Version
responseContent += QByteArray::number((qint32)response->status) + " "
+ response->StatusNames.value((qint32)response->status, "").toUtf8() + "\r\n"; // Status

// add headers
qint64 contentLength = 0;
for(auto itr = response->headers.begin(); itr != response->headers.end(); itr++) {
if(itr.key().toLower() == "content-length") contentLength = itr.value().toLongLong();
responseContent += itr.key() + ": " + itr.value() + "\r\n";
responseContent += itr.key().toUtf8() + ": " + itr.value().toUtf8() + "\r\n";
}

// add content length if not available
Expand Down
2 changes: 1 addition & 1 deletion src/jsonhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
QVariant JsonHelper::jsonPathGetImpl(QJsonValue data, QString path, bool showWarnings)
{
// json parse
QStringList splittedPath = path.split('.', QString::SkipEmptyParts);
QStringList splittedPath = path.split('.');
QStringList processed;
while(!splittedPath.isEmpty()) {
QString pathElement = splittedPath.takeFirst();
Expand Down
93 changes: 64 additions & 29 deletions src/telegrambot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ void TelegramBot::answerCallbackQuery(QString callbackQueryId, QString text, boo
this->callApiTemplate("answerCallbackQuery", params, response);
}

void TelegramBot::answerInlineQuery(const QString inlineQueryId,
QList<TelegramBotInlineQueryResult*> &results ,
bool is_personal,
int cacheTime,
QString switch_pm_text,
QString switch_pm_parameter,
QString url,
bool *response) {
QUrlQuery params;
params.addQueryItem("inline_query_id", inlineQueryId);
QJsonArray formedResults;
for (auto result : results) {
QJsonObject resultObj;
result->toJson(resultObj);
formedResults.append(resultObj);
delete result;
}
if (!switch_pm_parameter.isEmpty())
params.addQueryItem("switch_pm_parameter", switch_pm_parameter);

if (!switch_pm_text.isEmpty())
params.addQueryItem("switch_pm_text", switch_pm_text);

params.addQueryItem("is_personal", is_personal?"true":"false");

params.addQueryItem("results", QJsonDocument(formedResults).toJson(QJsonDocument::Compact));
if(!url.isNull()) params.addQueryItem("url", url);
if (cacheTime > 0) params.addQueryItem("cache_time", QString::number(cacheTime));

this->callApiTemplate("answerInlineQuery", params, response);
}


/*
* Message Functions
Expand All @@ -201,13 +233,14 @@ void TelegramBot::sendMessage(QVariant chatId, QString text, int replyToMessageI
params.addQueryItem("chat_id", chatId.toString());
params.addQueryItem("text", text);
if(flags && TelegramFlags::Markdown) params.addQueryItem("parse_mode", "Markdown");
else if(flags && TelegramFlags::MarkDownV2) params.addQueryItem("parse_mode", "MarkdownV2");
else if(flags && TelegramFlags::Html) params.addQueryItem("parse_mode", "HTML");
if(flags && TelegramFlags::DisableWebPagePreview) params.addQueryItem("disable_web_page_preview", "true");
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// call api
return this->callApiTemplate("sendMessage", params, response);
Expand All @@ -233,7 +266,7 @@ void TelegramBot::editMessageText(QVariant chatId, QVariant messageId, QString t

// only build inline keyboard
if(!(flags && TelegramFlags::ReplyKeyboardMarkup) && !(flags && TelegramFlags::ForceReply) && !(flags && TelegramFlags::ReplyKeyboardRemove)) {
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);
}

// call api
Expand All @@ -251,7 +284,7 @@ void TelegramBot::editMessageCaption(QVariant chatId, QVariant messageId, QStrin
if(!caption.isNull()) params.addQueryItem("caption", caption);

// only build inline keyboard
this->hanldeReplyMarkup(params, TelegramFlags(), keyboard);
this->handleReplyMarkup(params, TelegramFlags(), keyboard);

// call api
this->callApiTemplate("editMessageCaption", params, response);
Expand All @@ -267,7 +300,7 @@ void TelegramBot::editMessageReplyMarkup(QVariant chatId, QVariant messageId, Te
params.addQueryItem(isInlineMessageId ? "inline_message_id" : "message_id", messageId.toString());

// only build inline keyboard
this->hanldeReplyMarkup(params, TelegramFlags(), keyboard);
this->handleReplyMarkup(params, TelegramFlags(), keyboard);

// call api
this->callApiTemplate("editMessageReplyMarkup", params, response);
Expand All @@ -279,7 +312,7 @@ void TelegramBot::forwardMessage(QVariant targetChatId, QVariant fromChatId, qin
params.addQueryItem("chat_id", targetChatId.toString());
params.addQueryItem("from_chat_id", fromChatId.toString());
params.addQueryItem("message_id", QString::number(fromMessageId));
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");

this->callApiTemplate("forwardMessage", params, response);
}
Expand All @@ -301,11 +334,15 @@ void TelegramBot::sendPhoto(QVariant chatId, QVariant photo, QString caption, in
QUrlQuery params;
params.addQueryItem("chat_id", chatId.toString());
if(!caption.isNull()) params.addQueryItem("caption", caption);
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::Markdown) params.addQueryItem("parse_mode", "Markdown");
else if(flags && TelegramFlags::Html) params.addQueryItem("parse_mode", "HTML");
if(flags && TelegramFlags::MarkDownV2) params.addQueryItem("parse_mode", "MarkdownV2");

if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("photo", photo, params);
Expand All @@ -322,11 +359,11 @@ void TelegramBot::sendAudio(QVariant chatId, QVariant audio, QString caption, QS
if(duration >= 0) params.addQueryItem("duration", QString::number(duration));
if(!performer.isNull()) params.addQueryItem("performer", performer);
if(!title.isNull()) params.addQueryItem("title", title);
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("audio", audio, params);
Expand All @@ -340,11 +377,11 @@ void TelegramBot::sendDocument(QVariant chatId, QVariant document, QString capti
QUrlQuery params;
params.addQueryItem("chat_id", chatId.toString());
if(!caption.isNull()) params.addQueryItem("caption", caption);
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("document", document, params);
Expand All @@ -357,11 +394,11 @@ void TelegramBot::sendSticker(QVariant chatId, QVariant sticker, int replyToMess
{
QUrlQuery params;
params.addQueryItem("chat_id", chatId.toString());
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("sticker", sticker, params);
Expand All @@ -378,11 +415,11 @@ void TelegramBot::sendVideo(QVariant chatId, QVariant video, QString caption, in
if(duration >= 0) params.addQueryItem("duration", QString::number(duration));
if(width >= 0) params.addQueryItem("width", QString::number(width));
if(height >= 0) params.addQueryItem("height", QString::number(height));
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("video", video, params);
Expand All @@ -397,11 +434,11 @@ void TelegramBot::sendVoice(QVariant chatId, QVariant voice, QString caption, in
params.addQueryItem("chat_id", chatId.toString());
if(!caption.isNull()) params.addQueryItem("caption", caption);
if(duration >= 0) params.addQueryItem("duration", QString::number(duration));
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("voice", voice, params);
Expand All @@ -416,11 +453,11 @@ void TelegramBot::sendVideoNote(QVariant chatId, QVariant videoNote, int length,
params.addQueryItem("chat_id", chatId.toString());
if(length >= 0) params.addQueryItem("length", QString::number(length));
if(duration >= 0) params.addQueryItem("duration", QString::number(duration));
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// handle file
QHttpMultiPart* multiPart = this->handleFile("video_note", videoNote, params);
Expand All @@ -435,11 +472,11 @@ void TelegramBot::sendLocation(QVariant chatId, double latitude, double longitud
params.addQueryItem("chat_id", chatId.toString());
params.addQueryItem("latitude", QString::number(latitude));
params.addQueryItem("longitude", QString::number(longitude));
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// call api
this->callApiTemplate("sendLocation", params, response);
Expand All @@ -454,11 +491,11 @@ void TelegramBot::sendVenue(QVariant chatId, double latitude, double longitude,
params.addQueryItem("title", title);
params.addQueryItem("address", address);
if(!foursquareId.isNull()) params.addQueryItem("foursquare_id", foursquareId);
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// call api
this->callApiTemplate("sendVenue", params, response);
Expand All @@ -471,11 +508,11 @@ void TelegramBot::sendContact(QVariant chatId, QString phoneNumber, QString firs
params.addQueryItem("phone_number", phoneNumber);
params.addQueryItem("first_name", firstName);
if(!lastName.isNull()) params.addQueryItem("last_name", lastName);
if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true");
if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");
if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId));

// handle reply markup
this->hanldeReplyMarkup(params, flags, keyboard);
this->handleReplyMarkup(params, flags, keyboard);

// call api
this->callApiTemplate("sendContact", params, response);
Expand Down Expand Up @@ -754,8 +791,6 @@ QNetworkReply* TelegramBot::callApi(QString method, QUrlQuery params, bool delet
QUrl url(QString("https://api.telegram.org/bot%1/%2").arg(this->apiKey, method));
url.setQuery(params);

qDebug() << url;

// execute
QNetworkRequest request(url);
QNetworkReply* reply = multiPart ? this->aManager.post(request, multiPart) : this->aManager.get(request);
Expand Down Expand Up @@ -793,7 +828,7 @@ QHttpMultiPart* TelegramBot::createUploadFile(QString name, QString fileName, QB
return multiPart;
}

void TelegramBot::hanldeReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest &keyboard)
void TelegramBot::handleReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest &keyboard)
{
// handle types
QString replyMarkup;
Expand Down
23 changes: 12 additions & 11 deletions src/telegrambot.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,24 @@ class TelegramBot : public QObject

// Message
Markdown = 1 << 0,
Html = 1 << 1,
DisableWebPagePreview = 1 << 2,
DisableNotfication = 1 << 3,
MarkDownV2 = 1 << 1,
Html = 1 << 2,
DisableWebPagePreview = 1 << 3,
DisableNotification = 1 << 4,

// Keyboard Global
Selective = 1 << 4,
Selective = 1 << 5,

// ReplyKeyboardMarkup
ReplyKeyboardMarkup = 1 << 5,
ResizeKeyboard = 1 << 6,
OneTimeKeyboard = 1 << 7,
ReplyKeyboardMarkup = 1 << 6,
ResizeKeyboard = 1 << 7,
OneTimeKeyboard = 1 << 8,

// ReplyKeyboardRemove
ReplyKeyboardRemove = 1 << 8,
ReplyKeyboardRemove = 1 << 9,

// ForceReply
ForceReply = 1 << 9
ForceReply = 1 << 10
};

// Keyboard construction helpers
Expand Down Expand Up @@ -107,7 +108,7 @@ class TelegramBot : public QObject

// Callback Query Functions
void answerCallbackQuery(QString callbackQueryId, QString text = QString(), bool showAlert = false, int cacheTime = 0, QString url = QString(), bool* response = 0);

void answerInlineQuery(const QString inlineQueryId, QList<TelegramBotInlineQueryResult *> &results, bool is_personal = false, int cacheTime=0, QString switch_pm_text="", QString switch_pm_parameter="", QString url="", bool* response = 0);
// Message Functions
void sendMessage(QVariant chatId, QString text, int replyToMessageId = 0, TelegramFlags flags = TelegramFlags::NoFlag, TelegramKeyboardRequest keyboard = TelegramKeyboardRequest(), TelegramBotMessage* response = 0);
void editMessageText(QVariant chatId, QVariant messageId, QString text, TelegramFlags flags = TelegramFlags::NoFlag, TelegramKeyboardRequest keyboard = TelegramKeyboardRequest(), bool* response = 0);
Expand Down Expand Up @@ -164,7 +165,7 @@ class TelegramBot : public QObject

// helpers
QHttpMultiPart* createUploadFile(QString name, QString fileName, QByteArray& content, bool detectMimeType = false, QHttpMultiPart* multiPart = 0);
void hanldeReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest& keyboard);
void handleReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest& keyboard);
QHttpMultiPart* handleFile(QString fieldName, QVariant file, QUrlQuery& params, QHttpMultiPart* multiPart = 0);

// global data
Expand Down
10 changes: 8 additions & 2 deletions src/telegramdatainterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#include "jsonhelper.h"

struct TelegramBotObject
class TelegramBotObject
{
public:
virtual void fromJson(QJsonObject& object) = 0;
virtual void toJson(QJsonObject& object) {Q_UNUSED(object)};
virtual ~TelegramBotObject() {}
};

Expand All @@ -15,7 +17,11 @@ class JsonHelperT<T, typename std::enable_if<std::is_base_of<TelegramBotObject,
public:
static bool jsonPathGet(QJsonValue data, QString path, T& target, bool showWarnings = true)
{
QJsonObject object = showWarnings ? JsonHelper::jsonPathGet(data, path).toJsonObject() : JsonHelper::jsonPathGetSilent(data, path).toJsonObject();
QJsonObject object;
if (path.isEmpty())
object = data.toObject();
else
object = showWarnings ? JsonHelper::jsonPathGet(data, path).toJsonObject() : JsonHelper::jsonPathGetSilent(data, path).toJsonObject();
if(object.isEmpty()) return false;
target.fromJson(object);
return true;
Expand Down
Loading