support send photo message.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
		
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@@ -32,6 +32,11 @@ func main() {
 | 
				
			|||||||
			Usage:  "send telegram message",
 | 
								Usage:  "send telegram message",
 | 
				
			||||||
			EnvVar: "PLUGIN_MESSAGE",
 | 
								EnvVar: "PLUGIN_MESSAGE",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							cli.StringSliceFlag{
 | 
				
			||||||
 | 
								Name:   "photo",
 | 
				
			||||||
 | 
								Usage:  "send photo message",
 | 
				
			||||||
 | 
								EnvVar: "PLUGIN_PHOTO",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		cli.BoolFlag{
 | 
							cli.BoolFlag{
 | 
				
			||||||
			Name:   "debug",
 | 
								Name:   "debug",
 | 
				
			||||||
			Usage:  "enable debug message",
 | 
								Usage:  "enable debug message",
 | 
				
			||||||
@@ -121,6 +126,7 @@ func run(c *cli.Context) error {
 | 
				
			|||||||
			Debug:   c.Bool("debug"),
 | 
								Debug:   c.Bool("debug"),
 | 
				
			||||||
			To:      c.StringSlice("to"),
 | 
								To:      c.StringSlice("to"),
 | 
				
			||||||
			Message: c.StringSlice("message"),
 | 
								Message: c.StringSlice("message"),
 | 
				
			||||||
 | 
								Photo:   c.StringSlice("photo"),
 | 
				
			||||||
			Format:  c.String("format"),
 | 
								Format:  c.String("format"),
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										26
									
								
								plugin.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								plugin.go
									
									
									
									
									
								
							@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -35,6 +36,7 @@ type (
 | 
				
			|||||||
		Debug   bool
 | 
							Debug   bool
 | 
				
			||||||
		To      []string
 | 
							To      []string
 | 
				
			||||||
		Message []string
 | 
							Message []string
 | 
				
			||||||
 | 
							Photo   []string
 | 
				
			||||||
		Format  string
 | 
							Format  string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,6 +62,19 @@ func trimElement(keys []string) []string {
 | 
				
			|||||||
	return newKeys
 | 
						return newKeys
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fileExist(keys []string) []string {
 | 
				
			||||||
 | 
						var newKeys []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, value := range keys {
 | 
				
			||||||
 | 
							if _, err := os.Stat(value); os.IsNotExist(err) {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							newKeys = append(newKeys, value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return newKeys
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseID(keys []string) []int64 {
 | 
					func parseID(keys []string) []int64 {
 | 
				
			||||||
	var newKeys []int64
 | 
						var newKeys []int64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -104,11 +119,11 @@ func (p Plugin) Exec() error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// parse ids
 | 
						// parse ids
 | 
				
			||||||
	ids := parseID(p.Config.To)
 | 
						ids := parseID(p.Config.To)
 | 
				
			||||||
 | 
						photos := fileExist(trimElement(p.Config.Photo))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// send message.
 | 
						// send message.
 | 
				
			||||||
	for _, user := range ids {
 | 
						for _, user := range ids {
 | 
				
			||||||
		for _, value := range trimElement(message) {
 | 
							for _, value := range trimElement(message) {
 | 
				
			||||||
			log.Println(user)
 | 
					 | 
				
			||||||
			msg := tgbotapi.NewMessage(user, value)
 | 
								msg := tgbotapi.NewMessage(user, value)
 | 
				
			||||||
			msg.ParseMode = p.Config.Format
 | 
								msg.ParseMode = p.Config.Format
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -118,6 +133,15 @@ func (p Plugin) Exec() error {
 | 
				
			|||||||
				log.Println(err.Error())
 | 
									log.Println(err.Error())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for _, value := range photos {
 | 
				
			||||||
 | 
								msg := tgbotapi.NewPhotoUpload(user, value)
 | 
				
			||||||
 | 
								_, err := bot.Send(msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									log.Println(err.Error())
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -69,6 +69,7 @@ func TestSendMessage(t *testing.T) {
 | 
				
			|||||||
			Token:   os.Getenv("TELEGRAM_TOKEN"),
 | 
								Token:   os.Getenv("TELEGRAM_TOKEN"),
 | 
				
			||||||
			To:      []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
 | 
								To:      []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
 | 
				
			||||||
			Message: []string{"Test Telegram Chat Bot From Travis or Local", " "},
 | 
								Message: []string{"Test Telegram Chat Bot From Travis or Local", " "},
 | 
				
			||||||
 | 
								Photo:   []string{"tests/gophercolor.png", "1234", " "},
 | 
				
			||||||
			Debug:   false,
 | 
								Debug:   false,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -137,3 +138,13 @@ func TestParseID(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	assert.Equal(t, result, parseID(input))
 | 
						assert.Equal(t, result, parseID(input))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestCheckFileExist(t *testing.T) {
 | 
				
			||||||
 | 
						var input []string
 | 
				
			||||||
 | 
						var result []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						input = []string{"tests/gophercolor.png", "測試", "3"}
 | 
				
			||||||
 | 
						result = []string{"tests/gophercolor.png"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert.Equal(t, result, fileExist(input))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user