Skip to content

Commit 30e2823

Browse files
Patrick BanksPatrick Banks
authored andcommitted
Fixed bugs with Telegram notifications
1 parent 03af79b commit 30e2823

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [1.0.1] (2021-08-03)
2+
3+
### Bug Fixes
4+
5+
* **trader:** Telegram notifications now work, and /info command will work without configuring a valid Chat ID.
6+
17
## [1.0.0] (2021-08-01)
28

39
### Bug Fixes

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"required": false
4444
},
4545
"NOTIFIER_TELEGRAM_RECEIVER_ID": {
46-
"description": "Unique identifier for the target chat or username of the target channel (in the format @channelusername)",
46+
"description": "Unique identifier for the target chat (as a number) or username of the target channel (in the format @channelusername)",
4747
"required": false
4848
},
4949

docs/GETTING-STARTED.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ To add new Config Vars in Heroku:
135135
| NOTIFIER_GMAIL_APP_PASSWORD | string | Gmail password to get notifications |
136136
| IS_NOTIFIER_TELEGRAM_ENABLED | true / false | Selects if Telegram will be used
137137
| NOTIFIER_TELEGRAM_API_KEY | string | Telegram Key for your bot (To create one follow https://core.telegram.org/bots#6-botfather)
138-
| NOTIFIER_TELEGRAM_RECEIVER_ID | string | Unique identifier for the target chat or username of the target channel (in the format @channelusername)
138+
| NOTIFIER_TELEGRAM_RECEIVER_ID | string | Unique identifier for the target chat (as a number) or username of the target channel (in the format @channelusername)
139+
140+
*If you want to use Telegram and don't know your Chat ID, set the NOTIFIER_TELEGRAM_RECEIVER_ID to a dummy value, start the trader (you will see errors in the log), then use the /info command in Telegram and it will respond with the ID that you can use.*
139141

140142
**Trader Features**
141143
| Key | Value Format | Description |

src/trader/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function getDefault(): Readonly<any> {
3535
// Then talk to the @userinfobot at Telegram, and it will give you your personal receiver ID, and thats what you use for the TELEGRAM_RECEIVER_ID
3636
IS_NOTIFIER_TELEGRAM_ENABLED: bool({ default: false, desc: "Selects if Telegram will be used" }),
3737
NOTIFIER_TELEGRAM_API_KEY: str({ default: "", desc: "Telegram Key for your bot (To create one follow https://core.telegram.org/bots#6-botfather)" }),
38-
NOTIFIER_TELEGRAM_RECEIVER_ID: str({ default: "", desc: "Unique identifier for the target chat or username of the target channel (in the format @channelusername)" }),
38+
NOTIFIER_TELEGRAM_RECEIVER_ID: str({ default: "", desc: "Unique identifier for the target chat (as a number) or username of the target channel (in the format @channelusername)" }),
3939

4040
// Additional configuration options for trader features
4141
IS_BUY_QTY_FRACTION: bool({ default: false, desc: "Uses the 'Quantity to Buy' from the NBT Hub as a fraction of your wallet balance (e.g. 0.1 is 10%)" }),

src/trader/notifiers/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { basename } from "path/posix"
2+
import logger from "../../logger"
23
import { EntryType, PositionType, Signal, TradeOpen } from "../types/bva"
34
import { MessageType, Notifier, NotifierMessage } from "../types/notifier"
45
import { SourceType } from "../types/trader"
@@ -19,10 +20,14 @@ export default function initializeNotifiers(): Notifier {
1920

2021
// Sends notifications on all the different channels
2122
export function notifyAll(notifierMessage: NotifierMessage): Promise<void> {
22-
return new Promise((resolve) => {
23+
return new Promise((resolve, reject) => {
2324
Promise.all(
2425
notifiers.map((notifier) => notifier.notify(notifierMessage))
2526
).then(() => resolve())
27+
.catch(reason => {
28+
logger.error(reason)
29+
reject(reason)
30+
})
2631
})
2732
}
2833

src/trader/notifiers/telegram.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@ import TeleBot from "telebot"
33
import env from "../env"
44
import { Notifier, NotifierMessage } from "../types/notifier"
55
import { getTradeOpenList } from "../apis/bva"
6+
import logger from "../../logger"
67

78
let telBot: TeleBot
89

910
export default function (): Notifier {
1011
telBot = new TeleBot(env().NOTIFIER_TELEGRAM_API_KEY)
1112
telBot.on("/info", async (msg) => {
1213
const tradeOpenList = await getTradeOpenList().catch((reason) => {
14+
logger.error(reason)
1315
return Promise.reject(reason)
1416
})
1517
return notify({
1618
content:
1719
`Open Trades: ${tradeOpenList.length} - ${tradeOpenList
1820
.map((tradeOpen) => tradeOpen.symbol)
19-
.join(", ")}\n` + `Channel ID: ${msg.chat.id}`,
20-
})
21+
.join(", ")}\n` + `Channel Chat ID: ${msg.chat.id}`,
22+
}, msg.from.id)
2123
})
2224
telBot.on("start", async () => {
2325
await notify({
2426
content: "Trader Bot started!",
2527
}).catch((reason) => {
28+
logger.error(reason)
2629
return Promise.reject(reason)
2730
})
2831
})
@@ -33,21 +36,14 @@ export default function (): Notifier {
3336
}
3437
}
3538

36-
async function notify(notifierMessage: NotifierMessage): Promise<void> {
39+
async function notify(notifierMessage: NotifierMessage, fromId?: string): Promise<void> {
3740
if (!env().IS_NOTIFIER_TELEGRAM_ENABLED || !telBot) return
3841

39-
return new Promise((resolve, reject) => {
40-
try {
41-
telBot.sendMessage(
42-
env().NOTIFIER_TELEGRAM_RECEIVER_ID,
43-
notifierMessage.contentHtml || notifierMessage.content,
44-
{
45-
parseMode: "html",
46-
}
47-
)
48-
resolve()
49-
} catch (e) {
50-
reject(e)
51-
}
52-
})
42+
return telBot.sendMessage(
43+
fromId || env().NOTIFIER_TELEGRAM_RECEIVER_ID,
44+
notifierMessage.content,
45+
/*{
46+
parseMode: "html",
47+
}*/
48+
)
5349
}

0 commit comments

Comments
 (0)