From 1543cec5ccb407542865d24fc891f20b9f8efd5f Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 8 Feb 2021 23:17:58 +0100 Subject: [PATCH] New Plugin: Telegram --- debian/control | 10 + debian/nymea-plugin-telegram.install.in | 1 + nymea-plugins.pro | 1 + telegram/README.md | 21 ++ telegram/integrationplugintelegram.cpp | 182 ++++++++++++++++++ telegram/integrationplugintelegram.h | 53 +++++ telegram/integrationplugintelegram.json | 68 +++++++ telegram/meta.json | 14 ++ telegram/telegram.pro | 13 ++ telegram/telegram.svg | 1 + ...5b5c8-f2f1-4bcb-bf88-8d165ee06114-en_US.ts | 80 ++++++++ 11 files changed, 444 insertions(+) create mode 100644 debian/nymea-plugin-telegram.install.in create mode 100644 telegram/README.md create mode 100644 telegram/integrationplugintelegram.cpp create mode 100644 telegram/integrationplugintelegram.h create mode 100644 telegram/integrationplugintelegram.json create mode 100644 telegram/meta.json create mode 100644 telegram/telegram.pro create mode 100644 telegram/telegram.svg create mode 100644 telegram/translations/bc95b5c8-f2f1-4bcb-bf88-8d165ee06114-en_US.ts diff --git a/debian/control b/debian/control index a3c0e990..421b582e 100644 --- a/debian/control +++ b/debian/control @@ -700,6 +700,15 @@ Description: nymea.io plugin for Texas Instruments devices This package will install the nymea.io plugin for Texas Instruments devices +Package: nymea-plugin-telegram +Architecture: any +Depends: ${shlibs:Depends}, + ${misc:Depends}, + nymea-plugins-translations, +Description: nymea.io plugin for Telegram + This plugin allows nymea to send messages to telegram via the bot API. + + Package: nymea-plugin-tplink Architecture: any Depends: ${shlibs:Depends}, @@ -1160,6 +1169,7 @@ Depends: nymea-plugin-anel, nymea-plugin-lifx, nymea-plugin-mailnotification, nymea-plugin-texasinstruments, + nymea-plugin-telegram, nymea-plugin-nanoleaf, nymea-plugin-netatmo, nymea-plugin-networkdetector, diff --git a/debian/nymea-plugin-telegram.install.in b/debian/nymea-plugin-telegram.install.in new file mode 100644 index 00000000..029afb3f --- /dev/null +++ b/debian/nymea-plugin-telegram.install.in @@ -0,0 +1 @@ +usr/lib/@DEB_HOST_MULTIARCH@/nymea/plugins/libnymea_integrationplugintelegram.so diff --git a/nymea-plugins.pro b/nymea-plugins.pro index 30260ca7..58b0a144 100644 --- a/nymea-plugins.pro +++ b/nymea-plugins.pro @@ -57,6 +57,7 @@ PLUGIN_DIRS = \ tado \ tasmota \ tcpcommander \ + telegram \ texasinstruments \ tplink \ tuya \ diff --git a/telegram/README.md b/telegram/README.md new file mode 100644 index 00000000..8d6297f5 --- /dev/null +++ b/telegram/README.md @@ -0,0 +1,21 @@ +# Telegram + +This plugin allows to send message to Telegram via the Telegram bot API. + +https://core.telegram.org/bots/api + +## Usage + +In order to use the telegram plugin, a new bot must be created and the bot access token provided to nymea. + +Instructions to create a telegram bot: + +* Open a conversation to @BotFather + +* Send the message `/newbot` to BotFather and follow the instructions until BotFather hands out the token for your bot. + +* Now open a conversation to your bot and write it a message. The content of this message does not matter but it is important that at least one message has been sent to the bot in order for it to work. + +* Alternatively, add the bot to a group chat and send a message to the group mentioning the bot with @. The content of this message does not matter as long as it contains a mention for the bot. This is required for the bot to see the message and allow nymea recognizing the group. + +* Now return to nymea and set up a new telegram thing as usual, providing the token during the setup. diff --git a/telegram/integrationplugintelegram.cpp b/telegram/integrationplugintelegram.cpp new file mode 100644 index 00000000..b9ce27e6 --- /dev/null +++ b/telegram/integrationplugintelegram.cpp @@ -0,0 +1,182 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project is distributed in the hope that +* it will be useful, but WITHOUT ANY WARRANTY; without even the implied +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "integrationplugintelegram.h" +#include "plugininfo.h" + +#include "network/networkaccessmanager.h" + +#include + +IntegrationPluginTelegram::IntegrationPluginTelegram(QObject* parent): IntegrationPlugin (parent) +{ + +} + +IntegrationPluginTelegram::~IntegrationPluginTelegram() +{ + +} + +void IntegrationPluginTelegram::discoverThings(ThingDiscoveryInfo *info) +{ + QString token = info->params().paramValue(telegramDiscoveryTokenParamTypeId).toString(); + QUrl url(QString("https://api.telegram.org/bot%1/getUpdates").arg(token)); + QNetworkRequest request(url); + QNetworkReply *reply = hardwareManager()->networkManager()->get(request); + connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); + connect(reply, &QNetworkReply::finished, info, [=](){ + if (reply->error() != QNetworkReply::NoError) { + qCWarning(dcTelegram()) << "Error fetching channels from telegram API:" << reply->error() << reply->errorString(); + info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to Telegram.")); + return; + } + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qCWarning(dcTelegram()) << "Error parsing JSON reply from telegram API:" << error.error << error.errorString(); + info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Unexpected data from Telegram.")); + return; + } + if (!jsonDoc.toVariant().toMap().value("ok").toBool()) { + qCWarning(dcTelegram()) << "Json reply doesn't contain OK" << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented)); + info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("An error happened on the Telegram servers.")); + return; + } + QVariantList entries = jsonDoc.toVariant().toMap().value("result").toList(); + QList addedChats; + foreach (const QVariant &entry, entries) { + QVariantMap messageMap = entry.toMap().value("message").toMap(); + int chatId = messageMap.value("chat").toMap().value("id").toInt(); + if (addedChats.contains(chatId)) { + continue; + } + QString chatName = QString("%1 %2") + .arg(messageMap.value("chat").toMap().value("first_name").toString()) + .arg(messageMap.value("chat").toMap().value("last_name").toString()); + QString type = messageMap.value("chat").toMap().value("type").toString(); + if (type == "group") { + chatName = messageMap.value("chat").toMap().value("title").toString(); + } + ThingDescriptor descriptor(telegramThingClassId, chatName, type == "group" ? "Group" : "Private"); + ParamList params; + params << Param(telegramThingTokenParamTypeId, token); + params << Param(telegramThingChatIdParamTypeId, chatId); + descriptor.setParams(params); + + Thing *existingThing = myThings().findByParams(params); + if (existingThing) { + descriptor.setThingId(existingThing->id()); + } + addedChats.append(chatId); + info->addThingDescriptor(descriptor); + } + info->finish(Thing::ThingErrorNoError); + }); +} + +void IntegrationPluginTelegram::setupThing(ThingSetupInfo *info) +{ + Thing *thing = info->thing(); + + qCDebug(dcTelegram()) << "Setting up telegram chat" << thing->name() << thing->id().toString(); + + QString token = info->thing()->paramValue(telegramThingTokenParamTypeId).toString(); +// int chatId = info->thing()->paramValue(telegramThingChatIdParamTypeId).toInt(); + + QString url = QString("https://api.telegram.org/bot%1/getMe").arg(token); + QNetworkRequest request(url); + QNetworkReply *reply = hardwareManager()->networkManager()->get(request); + + connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); + + connect(reply, &QNetworkReply::finished, info, [reply, info]() { + if (reply->error() != QNetworkReply::NoError) { + qCWarning(dcTelegram()) << "Error fetching user profile:" << reply->errorString() << reply->error(); + //: Error setting up thing + info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to Telegram.")); + return; + } + + qCDebug(dcTelegram()) << "Telegram" << info->thing()->name() << info->thing()->id().toString() << "setup complete"; + info->finish(Thing::ThingErrorNoError); + }); +} + +void IntegrationPluginTelegram::executeAction(ThingActionInfo *info) +{ + Thing *thing = info->thing(); + Action action = info->action(); + + qCDebug(dcTelegram()) << "Executing action" << action.actionTypeId() << "for" << thing->name() << thing->id().toString(); + + QString token = info->thing()->paramValue(telegramThingTokenParamTypeId).toString(); + int chatId = info->thing()->paramValue(telegramThingChatIdParamTypeId).toInt(); + + QString title = action.paramValue(telegramNotifyActionTitleParamTypeId).toString(); + QString body = action.paramValue(telegramNotifyActionBodyParamTypeId).toString(); + QString message = title + "\n" + body; + + QString url = QString("https://api.telegram.org/bot%1/sendMessage?chat_id=%2&text=%3").arg(token).arg(chatId).arg(message); + + QNetworkRequest request(url); + + QNetworkReply *reply = hardwareManager()->networkManager()->get(request); + connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); + connect(reply, &QNetworkReply::finished, info, [reply, info]{ + if (reply->error() != QNetworkReply::NoError) { + qCWarning(dcTelegram()) << "Sending message failed for" << info->thing()->name() << info->thing()->id() << reply->errorString() << reply->error(); + emit info->finish(Thing::ThingErrorHardwareNotAvailable); + return; + } + + QByteArray data = reply->readAll(); + + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + if (error.error != QJsonParseError::NoError) { + qCWarning(dcTelegram()) << "Error reading reply from Telegram for" << info->thing()->name() << info->thing()->id().toString() << error.errorString(); + qCWarning(dcTelegram()) << qUtf8Printable(data); + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + QVariantMap replyMap = jsonDoc.toVariant().toMap(); + if (!replyMap.value("ok").toBool()) { + qCWarning(dcTelegram()) << "Error sending message." << info->thing()->name() << info->thing()->id().toString(); + info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("The Telegram bot account seems to be disabled.")); + return; + } + + qCDebug(dcTelegram()) << "Message sent successfully"; + info->finish(Thing::ThingErrorNoError); + }); +} + diff --git a/telegram/integrationplugintelegram.h b/telegram/integrationplugintelegram.h new file mode 100644 index 00000000..965d43d4 --- /dev/null +++ b/telegram/integrationplugintelegram.h @@ -0,0 +1,53 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project is distributed in the hope that +* it will be useful, but WITHOUT ANY WARRANTY; without even the implied +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef INTEGRATIONPLUGINPUSHBULLET_H +#define INTEGRATIONPLUGINPUSHBULLET_H + +#include "integrations/integrationplugin.h" + +class IntegrationPluginTelegram: public IntegrationPlugin +{ + Q_OBJECT + + Q_PLUGIN_METADATA(IID "io.nymea.IntegrationPlugin" FILE "integrationplugintelegram.json") + Q_INTERFACES(IntegrationPlugin) + +public: + explicit IntegrationPluginTelegram(QObject *parent = nullptr); + ~IntegrationPluginTelegram() override; + + void discoverThings(ThingDiscoveryInfo *info) override; + void setupThing(ThingSetupInfo *info) override; + void executeAction(ThingActionInfo *info) override; + +}; + +#endif diff --git a/telegram/integrationplugintelegram.json b/telegram/integrationplugintelegram.json new file mode 100644 index 00000000..e3015667 --- /dev/null +++ b/telegram/integrationplugintelegram.json @@ -0,0 +1,68 @@ +{ + "displayName": "Telegram", + "name": "Telegram", + "id": "bc95b5c8-f2f1-4bcb-bf88-8d165ee06114", + "vendors": [ + { + "displayName": "Telegram", + "name": "telegram", + "id": "45bb2595-4340-49df-8b88-8c6037c8d1c8", + "thingClasses": [ + { + "id": "f71bac7a-90e1-4810-9c31-531ac5f596fb", + "name": "telegram", + "displayName": "Telegram messages", + "createMethods": ["discovery", "user"], + "interfaces": ["notifications"], + "discoveryParamTypes": [ + { + "id": "2350bef6-d237-4bee-8e7e-6ab2c205f332", + "name": "token", + "displayName": "Bot access token", + "type": "QString" + } + ], + "paramTypes": [ + { + "id": "28e4c161-f77a-4baa-b415-dc693330756f", + "name": "token", + "displayName": "Bot access token", + "type": "QString" + }, + { + "id": "9c1f9d6c-e15a-4b76-a441-f1b96812cc28", + "name": "chatId", + "displayName": "Chat ID", + "type": "QString" + } + ], + "actionTypes": [ + { + "id": "36b4ddf9-1a05-46c6-b0c9-2c3b8c835d42", + "name": "notify", + "displayName": "notify", + "paramTypes": [ + { + "id": "891772ed-45bc-402e-828a-5349bedc29e5", + "name": "title", + "displayName": "title", + "type": "QString", + "inputType": "TextLine", + "defaultValue": "" + }, + { + "id": "374d059f-feab-4c49-8d91-e6066f80ed5a", + "name": "body", + "displayName": "body", + "type": "QString", + "inputType": "TextArea", + "defaultValue": "" + } + ] + } + ] + } + ] + } + ] +} diff --git a/telegram/meta.json b/telegram/meta.json new file mode 100644 index 00000000..015c29aa --- /dev/null +++ b/telegram/meta.json @@ -0,0 +1,14 @@ +{ + "title": "Telegram", + "tagline": "Send notifictions to Telegram.", + "icon": "telegram.svg", + "stability": "consumer", + "offline": false, + "technologies": [ + "cloud" + ], + "categories": [ + "online-service", + "notification" + ] +} diff --git a/telegram/telegram.pro b/telegram/telegram.pro new file mode 100644 index 00000000..eaabe276 --- /dev/null +++ b/telegram/telegram.pro @@ -0,0 +1,13 @@ +include(../plugins.pri) + +TARGET = $$qtLibraryTarget(nymea_integrationplugintelegram) + +QT+= network + +SOURCES += \ + integrationplugintelegram.cpp + +HEADERS += \ + integrationplugintelegram.h + + diff --git a/telegram/telegram.svg b/telegram/telegram.svg new file mode 100644 index 00000000..080818fa --- /dev/null +++ b/telegram/telegram.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/telegram/translations/bc95b5c8-f2f1-4bcb-bf88-8d165ee06114-en_US.ts b/telegram/translations/bc95b5c8-f2f1-4bcb-bf88-8d165ee06114-en_US.ts new file mode 100644 index 00000000..06d5bb1f --- /dev/null +++ b/telegram/translations/bc95b5c8-f2f1-4bcb-bf88-8d165ee06114-en_US.ts @@ -0,0 +1,80 @@ + + + + + IntegrationPluginTelegram + + + + Error connecting to Telegram. + Error setting up thing + + + + + Unexpected data from Telegram. + + + + + An error happened on the Telegram servers. + + + + + The Telegram bot account seems to be disabled. + + + + + Telegram + + + + Access token + The name of the ParamType (ThingClass: telegram, Type: discovery, ID: {2350bef6-d237-4bee-8e7e-6ab2c205f332}) +---------- +The name of the ParamType (ThingClass: telegram, Type: thing, ID: {28e4c161-f77a-4baa-b415-dc693330756f}) + + + + + Chat ID + The name of the ParamType (ThingClass: telegram, Type: thing, ID: {9c1f9d6c-e15a-4b76-a441-f1b96812cc28}) + + + + + + Telegram + The name of the vendor ({45bb2595-4340-49df-8b88-8c6037c8d1c8}) +---------- +The name of the plugin Telegram ({bc95b5c8-f2f1-4bcb-bf88-8d165ee06114}) + + + + + Telegram messages + The name of the ThingClass ({f71bac7a-90e1-4810-9c31-531ac5f596fb}) + + + + + body + The name of the ParamType (ThingClass: telegram, ActionType: notify, ID: {374d059f-feab-4c49-8d91-e6066f80ed5a}) + + + + + notify + The name of the ActionType ({36b4ddf9-1a05-46c6-b0c9-2c3b8c835d42}) of ThingClass telegram + + + + + title + The name of the ParamType (ThingClass: telegram, ActionType: notify, ID: {891772ed-45bc-402e-828a-5349bedc29e5}) + + + +