-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
239 lines (195 loc) · 6.63 KB
/
telegram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"math/bits"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/text/message"
)
type TelegramResponse struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Parameters struct {
RetryAfter int `json:"retry_after"`
} `json:"parameters"`
}
// sendTextToTelegramChat sends a text message to the Telegram chat identified by its chat Id
func sendTextToTelegramChat(botToken string, chatId int, chatThreadId string, text string) (string, int, error) {
//log.Printf("Sending to chat_id: %d\n Message:\n %s\n", chatId, text)
var telegramApi string = "https://api.telegram.org/bot" + botToken + "/sendMessage"
var urlValues map[string][]string
if chatThreadId == "" {
urlValues = url.Values{
"chat_id": {strconv.Itoa(chatId)},
"text": {text},
"parse_mode": {"HTML"},
}
} else {
urlValues = url.Values{
"chat_id": {strconv.Itoa(chatId)},
"text": {text},
"parse_mode": {"HTML"},
"message_thread_id": {chatThreadId},
}
}
response, err := http.PostForm(
telegramApi,
urlValues)
if err != nil {
log.Printf("error when posting text to the chat: %s", err.Error())
return "", 1, err
}
defer response.Body.Close()
var bodyBytes, errRead = io.ReadAll(response.Body)
if errRead != nil {
log.Printf("error in parsing telegram answer %s", errRead.Error())
return "", 1, err
}
bodyString := string(bodyBytes)
//log.Printf("Body of Telegram Response:\n %s", bodyString)
var tgr TelegramResponse
json.Unmarshal(bodyBytes, &tgr)
if !tgr.Ok {
return "", tgr.Parameters.RetryAfter, fmt.Errorf(tgr.Description)
}
return bodyString, 1, nil
}
func prettyPrintBlock(block BlockchainBlock, cfg Config) (prettyPrint string) {
p := message.NewPrinter(NUMBER_FORMAT)
//// add block number
blocknumber := p.Sprintf("%d", block.Header.Seq)
prettyPrint = fmt.Sprintf("<b>Block: %s</b>\n", blocknumber)
//// add transaction
txid := block.Body.Txns[0].Txid
prettyPrint = prettyPrint + fmt.Sprintf("TXN: %s\n", txid[0:4]+"..."+txid[len(txid)-4:])
////add Coin Whale Emoji
var fCoinsInput float64
fCoinsInputOwner := block.Body.Txns[0].Inputs[0].Owner
for _, input := range block.Body.Txns[0].Inputs {
_fCoins, err := strconv.ParseFloat(input.Coins, 64)
if err != nil {
//Handle error
}
fCoinsInput = fCoinsInput + _fCoins
}
var fCoinsOutput float64
for _, output := range block.Body.Txns[0].Outputs {
if output.Dst == fCoinsInputOwner {
_fCoins, err := strconv.ParseFloat(output.Coins, 64)
if err != nil {
//Handle error
}
fCoinsOutput = fCoinsOutput + _fCoins
}
}
fCoins := fCoinsInput - fCoinsOutput
if cfg.Emojis.PrintCoinEmojis {
symbols, _ := bits.Div(0, uint(fCoins), uint(cfg.Emojis.CoinEmojiDivisor))
for i := 0; i < int(symbols); i++ {
if cfg.Emojis.CoinCustomEmojiTelegramID != "" {
prettyPrint = prettyPrint + fmt.Sprintf("<tg-emoji emoji-id=%s>%s</tg-emoji>", cfg.Emojis.CoinCustomEmojiTelegramID, cfg.Emojis.CoinEmoji)
} else {
prettyPrint = prettyPrint + cfg.Emojis.CoinEmoji
}
}
if symbols != 0 {
prettyPrint = prettyPrint + "\n"
}
}
////add Coins
coins := p.Sprintf("%f", fCoins)
if strings.HasSuffix(coins, ".000000") {
prettyPrint = prettyPrint + fmt.Sprintf("%s: %s\n", cfg.FiberNode.FiberCoinTicker, coins[:len(coins)-7])
} else {
prettyPrint = prettyPrint + fmt.Sprintf("%s: %s\n", cfg.FiberNode.FiberCoinTicker, coins[:len(coins)-(6-cfg.FiberNode.FiberPrintPrecision)])
}
////add SCH
var hoursOutput int
hoursInputOwner := block.Body.Txns[0].Inputs[0].Owner
for _, output := range block.Body.Txns[0].Outputs {
if output.Dst != hoursInputOwner {
hoursOutput = hoursOutput + output.Hours
}
}
hours := p.Sprintf("%d", hoursOutput)
prettyPrint = prettyPrint + fmt.Sprintf("%s: %s %s\n", cfg.FiberNode.FiberHoursTicker, hours, getHoursEmoji(hoursOutput, cfg))
//// add FEE
fee := p.Sprintf("%d", block.Header.Fee)
if cfg.Emojis.PrintFeeEmoji {
prettyPrint = prettyPrint + fmt.Sprintf("FEE: %s %s %s %s\n", cfg.Emojis.FeeEmoji, fee, cfg.FiberNode.FiberHoursTicker, getHoursEmoji(block.Header.Fee, cfg))
} else {
prettyPrint = prettyPrint + fmt.Sprintf("FEE: %s %s\n", fee, cfg.FiberNode.FiberHoursTicker)
}
////add SND
inp := block.Body.Txns[0].Inputs[0].Owner
vHasAlias, alias, emoji, emoji_id := hasAlias(inp, cfg)
if vHasAlias {
if !cfg.Addressbook.PrintAliasEmoji {
emoji = ""
} else if emoji_id != "" {
emoji = fmt.Sprintf("<tg-emoji emoji-id=%s>%s</tg-emoji>", emoji_id, emoji)
}
prettyPrint = prettyPrint + fmt.Sprintf("SND: %s #%s \n", emoji, alias)
} else {
prettyPrint = prettyPrint + fmt.Sprintf("SND: %s \n", inp[0:4]+"..."+inp[len(inp)-4:])
}
////add RCV
inputOwner := block.Body.Txns[0].Inputs[0].Owner
var outputs int
for _, output := range block.Body.Txns[0].Outputs {
if output.Dst != inputOwner {
outputs = outputs + 1
}
}
outp := block.Body.Txns[0].Outputs[0].Dst
if outputs > 1 {
prettyPrint = prettyPrint + fmt.Sprintf("RCV: %s <i>(+ %d more)</i>", outp[0:4]+"..."+outp[len(outp)-4:], outputs-1)
} else {
vHasAlias, alias, emoji, emoji_id := hasAlias(outp, cfg)
if vHasAlias {
if !cfg.Addressbook.PrintAliasEmoji {
emoji = ""
} else if emoji_id != "" {
emoji = fmt.Sprintf("<tg-emoji emoji-id=%s>%s</tg-emoji>", emoji_id, emoji)
}
prettyPrint = prettyPrint + fmt.Sprintf("RCV: %s #%s ", emoji, alias)
} else {
prettyPrint = prettyPrint + fmt.Sprintf("RCV: %s ", outp[0:4]+"..."+outp[len(outp)-4:])
}
}
prettyPrint = prettyPrint + "\n"
////add Block-Details
prettyPrint = prettyPrint + "_____________________\n"
datetime := time.Unix(int64(block.Header.Timestamp), 0).UTC().Format(time.RFC822)
prettyPrint = prettyPrint + fmt.Sprintf("<i>%s</i>\n", datetime)
prettyPrint = prettyPrint + fmt.Sprintf("<a href='%s/app/transaction/%s'>Explorer</a>", cfg.Explorer.PublicURL, block.Body.Txns[0].Txid)
return prettyPrint
}
func getHoursEmoji(hours int, cfg Config) (emoji string) {
amount := cfg.Emojis.HoursEmojiMultiplicator
if hours >= (1000000 * amount) {
emoji = cfg.Emojis.HoursEmoji1M
} else if hours >= (500000 * amount) {
emoji = cfg.Emojis.HoursEmoji500K
} else if hours >= (100000 * amount) {
emoji = cfg.Emojis.HoursEmoji100K
} else if hours >= (10000 * amount) {
emoji = cfg.Emojis.HoursEmoji10K
} else if hours >= (1000 * amount) {
emoji = cfg.Emojis.HoursEmoji1K
} else if hours >= (100 * amount) {
emoji = cfg.Emojis.HoursEmoji100
} else if hours >= (10 * amount) {
emoji = cfg.Emojis.HoursEmoji10
} else if hours >= (1 * amount) {
emoji = cfg.Emojis.HoursEmoji1
}
return emoji
}