2016-10-04 16:45:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-10-07 09:34:31 +03:00
|
|
|
"os"
|
2016-10-04 16:54:11 +03:00
|
|
|
"strconv"
|
2016-10-04 16:45:49 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/telegram-bot-api.v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Repo information.
|
|
|
|
Repo struct {
|
|
|
|
Owner string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build information.
|
|
|
|
Build struct {
|
|
|
|
Event string
|
|
|
|
Number int
|
|
|
|
Commit string
|
|
|
|
Message string
|
|
|
|
Branch string
|
|
|
|
Author string
|
|
|
|
Status string
|
|
|
|
Link string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config for the plugin.
|
|
|
|
Config struct {
|
2016-10-07 16:50:42 +03:00
|
|
|
Token string
|
|
|
|
Debug bool
|
|
|
|
To []string
|
|
|
|
Message []string
|
|
|
|
Photo []string
|
|
|
|
Document []string
|
2016-10-09 04:56:31 +03:00
|
|
|
Sticker []string
|
2016-10-07 16:50:42 +03:00
|
|
|
Format string
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin values.
|
|
|
|
Plugin struct {
|
|
|
|
Repo Repo
|
|
|
|
Build Build
|
|
|
|
Config Config
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func trimElement(keys []string) []string {
|
|
|
|
var newKeys []string
|
|
|
|
|
|
|
|
for _, value := range keys {
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
if len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newKeys = append(newKeys, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
|
|
|
|
2016-10-07 09:34:31 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
func parseID(keys []string) []int64 {
|
|
|
|
var newKeys []int64
|
|
|
|
|
|
|
|
for _, value := range keys {
|
|
|
|
id, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newKeys = append(newKeys, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes the plugin.
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var message []string
|
|
|
|
if len(p.Config.Message) > 0 {
|
|
|
|
message = p.Config.Message
|
|
|
|
} else {
|
|
|
|
message = p.Message(p.Repo, p.Build)
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:09:10 +03:00
|
|
|
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2016-10-07 17:00:09 +03:00
|
|
|
log.Println("Initialize New Bot Error:", err.Error())
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:23:13 +03:00
|
|
|
bot.Debug = p.Config.Debug
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
// parse ids
|
|
|
|
ids := parseID(p.Config.To)
|
2016-10-07 09:34:31 +03:00
|
|
|
photos := fileExist(trimElement(p.Config.Photo))
|
2016-10-07 16:50:42 +03:00
|
|
|
documents := fileExist(trimElement(p.Config.Document))
|
2016-10-09 04:56:31 +03:00
|
|
|
stickers := fileExist(trimElement(p.Config.Sticker))
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
// send message.
|
|
|
|
for _, user := range ids {
|
|
|
|
for _, value := range trimElement(message) {
|
|
|
|
msg := tgbotapi.NewMessage(user, value)
|
2016-10-06 19:41:28 +03:00
|
|
|
msg.ParseMode = p.Config.Format
|
2016-10-07 16:50:42 +03:00
|
|
|
p.Send(bot, msg)
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
|
|
|
|
for _, value := range photos {
|
|
|
|
msg := tgbotapi.NewPhotoUpload(user, value)
|
2016-10-07 16:50:42 +03:00
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
|
2016-10-07 16:50:42 +03:00
|
|
|
for _, value := range documents {
|
|
|
|
msg := tgbotapi.NewDocumentUpload(user, value)
|
|
|
|
p.Send(bot, msg)
|
2016-10-07 09:34:31 +03:00
|
|
|
}
|
2016-10-09 04:56:31 +03:00
|
|
|
|
|
|
|
for _, value := range stickers {
|
|
|
|
msg := tgbotapi.NewStickerUpload(user, value)
|
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 16:50:42 +03:00
|
|
|
// Send bot message.
|
|
|
|
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) {
|
|
|
|
_, err := bot.Send(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// Message is plugin default message.
|
|
|
|
func (p Plugin) Message(repo Repo, build Build) []string {
|
|
|
|
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
|
|
|
|
build.Status,
|
|
|
|
build.Link,
|
|
|
|
build.Branch,
|
|
|
|
build.Message,
|
|
|
|
build.Author,
|
|
|
|
)}
|
|
|
|
}
|