Telegram: Fix small cosmetic issue during setup and improve readme
parent
68ca8605f3
commit
c571335906
|
|
@ -10,12 +10,25 @@ In order to use the telegram plugin, a new bot must be created and the bot acces
|
||||||
|
|
||||||
Instructions to create a telegram bot:
|
Instructions to create a telegram bot:
|
||||||
|
|
||||||
* Open a conversation to @BotFather
|
### Step 1: Creating a bot for nymea
|
||||||
|
|
||||||
|
* Open a conversation to @BotFather
|
||||||
* Send the message `/newbot` to BotFather and follow the instructions until BotFather hands out the token for your bot.
|
* 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.
|
### Step 2: Let the bot know of your chats
|
||||||
|
Depending on whether you want the bot to send messages to a single user or to a group chat, the next step differs. It is also possible to have both of them with the same bot.
|
||||||
|
|
||||||
* Alternatively, add the bot to a group chat and send a message to the group mentioning the bot with @<bot_name>. 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.
|
#### Option 1: Sending private messages to a Telegram user
|
||||||
|
|
||||||
* Now return to nymea and set up a new telegram thing as usual, providing the token during the setup.
|
* 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 from every user you want to send messages to.
|
||||||
|
|
||||||
|
#### Option 2: Sending messages to Telegram group chats
|
||||||
|
|
||||||
|
* Make sure Bot groupy privacy is turned off for your bot to receive group messages. Go to @BotFather, send `/mybots`, and find your way to turn group privacy off for your bot.
|
||||||
|
* Add the bot to a group chat and send a message to the group.
|
||||||
|
|
||||||
|
### Step 3: Adding the bot to nymea
|
||||||
|
|
||||||
|
* Now return to nymea and set up a new telegram thing. Nymea will ask you for the bot token in the first step. Paste the token and press next. Nymea should now show a list of all the chats the bot knows about. Pick the chat you want to send messages to. Repeat for adding multiple chats.
|
||||||
|
|
||||||
|
Note: After setting up a group chat in nymea, you can turn on group privacy for the bot again. Except during setup, it is not necessary for the bot to be able to read group messages.
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ IntegrationPluginTelegram::~IntegrationPluginTelegram()
|
||||||
|
|
||||||
void IntegrationPluginTelegram::discoverThings(ThingDiscoveryInfo *info)
|
void IntegrationPluginTelegram::discoverThings(ThingDiscoveryInfo *info)
|
||||||
{
|
{
|
||||||
QString token = info->params().paramValue(telegramDiscoveryTokenParamTypeId).toString();
|
QString token = info->params().paramValue(telegramDiscoveryTokenParamTypeId).toString().trimmed();
|
||||||
QUrl url(QString("https://api.telegram.org/bot%1/getUpdates").arg(token));
|
QUrl url(QString("https://api.telegram.org/bot%1/getUpdates").arg(token));
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||||
|
|
@ -70,20 +70,32 @@ void IntegrationPluginTelegram::discoverThings(ThingDiscoveryInfo *info)
|
||||||
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("An error happened on the Telegram servers."));
|
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("An error happened on the Telegram servers."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
qCDebug(dcTelegram()) << "Discovery data:" << qUtf8Printable(jsonDoc.toJson());
|
||||||
|
|
||||||
QVariantList entries = jsonDoc.toVariant().toMap().value("result").toList();
|
QVariantList entries = jsonDoc.toVariant().toMap().value("result").toList();
|
||||||
QList<int> addedChats;
|
QList<int> addedChats;
|
||||||
foreach (const QVariant &entry, entries) {
|
foreach (const QVariant &entry, entries) {
|
||||||
QVariantMap messageMap = entry.toMap().value("message").toMap();
|
QVariantMap entryMap = entry.toMap();
|
||||||
int chatId = messageMap.value("chat").toMap().value("id").toInt();
|
QVariantMap chatMap;
|
||||||
|
if (entryMap.contains("message")) {
|
||||||
|
chatMap = entry.toMap().value("message").toMap().value("chat").toMap();
|
||||||
|
} else if (entryMap.contains("my_chat_member")) {
|
||||||
|
chatMap = entry.toMap().value("my_chat_member").toMap().value("chat").toMap();
|
||||||
|
} else {
|
||||||
|
qCWarning(dcTelegram()) << "Neither message nor my_chat_member found in entry. Skipping:" << qUtf8Printable(QJsonDocument::fromVariant(entryMap).toJson());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int chatId = chatMap.value("id").toInt();
|
||||||
if (addedChats.contains(chatId)) {
|
if (addedChats.contains(chatId)) {
|
||||||
|
qCDebug(dcTelegram()) << "Skipping chat" << chatId << "(Already added to discovery results)";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QString chatName = QString("%1 %2")
|
QString chatName = QString("%1 %2")
|
||||||
.arg(messageMap.value("chat").toMap().value("first_name").toString())
|
.arg(chatMap.value("first_name").toString())
|
||||||
.arg(messageMap.value("chat").toMap().value("last_name").toString());
|
.arg(chatMap.value("last_name").toString());
|
||||||
QString type = messageMap.value("chat").toMap().value("type").toString();
|
QString type = chatMap.value("type").toString();
|
||||||
if (type == "group") {
|
if (type == "group") {
|
||||||
chatName = messageMap.value("chat").toMap().value("title").toString();
|
chatName = chatMap.value("title").toString();
|
||||||
}
|
}
|
||||||
ThingDescriptor descriptor(telegramThingClassId, chatName, type == "group" ? "Group" : "Private");
|
ThingDescriptor descriptor(telegramThingClassId, chatName, type == "group" ? "Group" : "Private");
|
||||||
ParamList params;
|
ParamList params;
|
||||||
|
|
@ -93,9 +105,11 @@ void IntegrationPluginTelegram::discoverThings(ThingDiscoveryInfo *info)
|
||||||
|
|
||||||
Thing *existingThing = myThings().findByParams(params);
|
Thing *existingThing = myThings().findByParams(params);
|
||||||
if (existingThing) {
|
if (existingThing) {
|
||||||
|
qCDebug(dcTelegram()) << "Chat id" << chatId << "is already added as a thing.";
|
||||||
descriptor.setThingId(existingThing->id());
|
descriptor.setThingId(existingThing->id());
|
||||||
}
|
}
|
||||||
addedChats.append(chatId);
|
addedChats.append(chatId);
|
||||||
|
qCDebug(dcTelegram()) << "Adding chat" << chatId << descriptor.title() << descriptor.description();
|
||||||
info->addThingDescriptor(descriptor);
|
info->addThingDescriptor(descriptor);
|
||||||
}
|
}
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@
|
||||||
#define INTEGRATIONPLUGINPUSHBULLET_H
|
#define INTEGRATIONPLUGINPUSHBULLET_H
|
||||||
|
|
||||||
#include "integrations/integrationplugin.h"
|
#include "integrations/integrationplugin.h"
|
||||||
|
#include "extern-plugininfo.h"
|
||||||
|
|
||||||
class IntegrationPluginTelegram: public IntegrationPlugin
|
class IntegrationPluginTelegram: public IntegrationPlugin
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue