Skip to content

Commit

Permalink
* (Apollon77) Add new JSON states communication.responseJson and comm…
Browse files Browse the repository at this point in the history
…unication.responseSilentJson to also accept json structures (stringified!) to send messages
  • Loading branch information
Apollon77 committed Mar 19, 2022
1 parent db7b07d commit 6dbacc9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ msg.payload = {
"parse_mode": "MarkdownV2"
}
```
Before sending it to `telegram.INSTANCE.communicate.responseJson you need to stringify the object!`

<!--
Placeholder for the next version (at the beginning of the line):
Expand All @@ -707,6 +708,7 @@ msg.payload = {
## Changelog

### __WORK IN PROGRESS__
* (Apollon77) Add new JSON states communication.responseJson and communication.responseSilentJson to also accept json structures (stringified!) to send messages
* (Apollon77) Try to prevent adapter crashes when internet is not available
* (Apollon77) Add Sentry for crash reporting

Expand Down
24 changes: 24 additions & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,30 @@
},
"native": {}
},
{
"_id": "communicate.responseJson",
"type": "state",
"common": {
"role": "json",
"name": "Send text through telegram with notification with json definition",
"type": "string",
"read": true,
"write": true
},
"native": {}
},
{
"_id": "communicate.responseSilentJson",
"type": "state",
"common": {
"role": "json",
"name": "Send text through telegram silently with json definition",
"type": "string",
"read": true,
"write": true
},
"native": {}
},
{
"_id": "communicate.users",
"type": "state",
Expand Down
24 changes: 24 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ function startAdapter(options) {
// Send to someone this message
sendMessage(state.val, null, null, {disable_notification: true})
.then(data => adapter.setState('communicate.responseSilent', state.val, true));
} else
if (id.endsWith('communicate.responseJson')) {
try {
const val = JSON.parse(state.val);
// Send to someone this message
sendMessage(val)
.then(data => adapter.setState('communicate.responseJson', state.val, true));
} catch (err) {
adapter.log.error(`could not parse Json in responseJon state: ${err.message}`);
}
} else
if (id.endsWith('communicate.responseSilentJson')) {
try {
const val = JSON.parse(state.val);
// Send to someone this message
sendMessage(val, null, null, {disable_notification: true})
.then(data => adapter.setState('communicate.responseSilent', state.val, true));
} catch (err) {
adapter.log.error(`could not parse Json in responseSilentJon state: ${err.message}`);
}
}
} else {
if (commands[id] && commands[id].report) {
Expand Down Expand Up @@ -2117,11 +2137,15 @@ async function main() {
await adapter.subscribeStatesAsync('communicate.request');
await adapter.subscribeStatesAsync('communicate.response');
await adapter.subscribeStatesAsync('communicate.responseSilent');
await adapter.subscribeStatesAsync('communicate.responseJson');
await adapter.subscribeStatesAsync('communicate.responseSilentJson');

// clear states
await adapter.setStateAsync('communicate.request', '', true);
await adapter.setStateAsync('communicate.response', '', true);
await adapter.setStateAsync('communicate.responseSilent', '', true);
await adapter.setStateAsync('communicate.responseJson', '', true);
await adapter.setStateAsync('communicate.responseSilentJson', '', true);
await adapter.setStateAsync('communicate.pathFile', '', true);

adapter.config.password = adapter.config.password || '';
Expand Down

0 comments on commit 6dbacc9

Please sign in to comment.