feat(logs): show logs if send error. (#55)

* feat(logs): show logs if send error.
This commit is contained in:
Bo-Yi Wu 2017-12-18 15:16:10 +08:00 committed by GitHub
parent 6a0370ab67
commit f22326b26a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"log"
"os"
"github.com/joho/godotenv"
@ -183,7 +184,10 @@ func main() {
EnvVar: "ENV_FILE",
},
}
app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Println(err)
}
}
func run(c *cli.Context) error {

View File

@ -209,8 +209,6 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
func (p Plugin) Exec() error {
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
log.Println("missing telegram token or user list")
return errors.New("missing telegram token or user list")
}
@ -223,9 +221,12 @@ func (p Plugin) Exec() error {
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
if err != nil {
log.Println("Initialize New Bot Error:", err.Error())
// enable bot debug mode
if p.Config.Debug {
bot.Debug = true
}
if err != nil {
return err
}
@ -271,39 +272,53 @@ func (p Plugin) Exec() error {
msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format
msg.DisableWebPagePreview = !p.Config.WebPreview
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range photos {
msg := tgbotapi.NewPhotoUpload(user, value)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range documents {
msg := tgbotapi.NewDocumentUpload(user, value)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range stickers {
msg := tgbotapi.NewStickerUpload(user, value)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range audios {
msg := tgbotapi.NewAudioUpload(user, value)
msg.Title = "Audio Message."
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range voices {
msg := tgbotapi.NewVoiceUpload(user, value)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range videos {
msg := tgbotapi.NewVideoUpload(user, value)
msg.Caption = "Video Message"
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range locations {
@ -314,7 +329,9 @@ func (p Plugin) Exec() error {
}
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range venues {
@ -325,7 +342,9 @@ func (p Plugin) Exec() error {
}
msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude)
p.Send(bot, msg)
if err := p.Send(bot, msg); err != nil {
return err
}
}
}
@ -333,12 +352,16 @@ func (p Plugin) Exec() error {
}
// Send bot message.
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) {
_, err := bot.Send(msg)
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
message, err := bot.Send(msg)
if err != nil {
log.Println(err.Error())
if p.Config.Debug == true {
log.Println("=====================")
log.Printf("Response Message: %#v\n", message)
log.Println("=====================")
}
return err
}
// Message is plugin default message.

View File

@ -88,17 +88,17 @@ func TestSendMessage(t *testing.T) {
}
err := plugin.Exec()
assert.Nil(t, err)
assert.NotNil(t, err)
plugin.Config.Format = "markdown"
plugin.Config.Message = []string{"Test escape under_score"}
err = plugin.Exec()
assert.Nil(t, err)
assert.NotNil(t, err)
// disable message
plugin.Config.Message = []string{}
err = plugin.Exec()
assert.Nil(t, err)
assert.NotNil(t, err)
}
func TestBotError(t *testing.T) {