update 'speed' 10 times

This commit is contained in:
pyed 2016-07-03 08:12:11 +03:00
parent 2830d64699
commit 019846a99e

View File

@ -117,15 +117,16 @@ func init() {
func main() { func main() {
for update := range Updates { for update := range Updates {
// ignore anyone other than 'master'
if strings.ToLower(update.Message.From.UserName) != strings.ToLower(Master) {
continue
}
// ignore edited messages // ignore edited messages
if update.Message == nil { if update.Message == nil {
continue continue
} }
// ignore anyone other than 'master'
if strings.ToLower(update.Message.From.UserName) != strings.ToLower(Master) {
continue
}
// tokenize the update // tokenize the update
tokens := strings.Split(update.Message.Text, " ") tokens := strings.Split(update.Message.Text, " ")
command := strings.ToLower(tokens[0]) command := strings.ToLower(tokens[0])
@ -209,7 +210,7 @@ func main() {
case "speed", "/speed": case "speed", "/speed":
// print current download and upload speeds // print current download and upload speeds
go speed(&update) go speed(update)
case "count", "/count": case "count", "/count":
// sends current torrents count per status // sends current torrents count per status
@ -944,15 +945,34 @@ func stats(ud *tgbotapi.Update) {
} }
// speed will echo back the current download and upload speeds // speed will echo back the current download and upload speeds
func speed(ud *tgbotapi.Update) { func speed(ud tgbotapi.Update) {
stats, err := Client.GetStats() // keep track of the returned message ID from 'send()' to edit the message.
if err != nil { var msgID int
send("speed: "+err.Error(), ud.Message.Chat.ID) for i := 0; i < 10; i++ {
return stats, err := Client.GetStats()
if err != nil {
send("speed: "+err.Error(), ud.Message.Chat.ID)
return
}
msg := fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(stats.DownloadSpeed), humanize.Bytes(stats.UploadSpeed))
// if we haven't send a message, send it and save the message ID to edit it the next iteration
if msgID == 0 {
msgID = send(msg, ud.Message.Chat.ID)
time.Sleep(time.Second * 2)
continue
}
// we have sent the message, let's update.
editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, msg)
Bot.Send(editConf)
time.Sleep(time.Second * 2)
} }
msg := fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(stats.DownloadSpeed), humanize.Bytes(stats.UploadSpeed)) // after the 10th iteration, show dashes to indicate that we are done updating.
send(msg, ud.Message.Chat.ID) editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, "↓ - B ↑ - B")
Bot.Send(editConf)
} }
// count returns current torrents count per status // count returns current torrents count per status
@ -1049,8 +1069,8 @@ func version(ud *tgbotapi.Update) {
send(fmt.Sprintf("Transmission %s\nTransmission-telegram %s", Client.Version(), VERSION), ud.Message.Chat.ID) send(fmt.Sprintf("Transmission %s\nTransmission-telegram %s", Client.Version(), VERSION), ud.Message.Chat.ID)
} }
// send takes a chat id and a message to send. // send takes a chat id and a message to send, returns the message id of the send message
func send(text string, chatID int64) { func send(text string, chatID int64) int {
// set typing action // set typing action
action := tgbotapi.NewChatAction(chatID, tgbotapi.ChatTyping) action := tgbotapi.NewChatAction(chatID, tgbotapi.ChatTyping)
Bot.Send(action) Bot.Send(action)
@ -1076,7 +1096,11 @@ LenCheck:
// if msgRuneCount < 4096, send it normally // if msgRuneCount < 4096, send it normally
msg := tgbotapi.NewMessage(chatID, text) msg := tgbotapi.NewMessage(chatID, text)
msg.DisableWebPagePreview = true msg.DisableWebPagePreview = true
if _, err := Bot.Send(msg); err != nil {
resp, err := Bot.Send(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "send error: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "send error: %s\n", err.Error())
} }
return resp.MessageID
} }