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"
|
|
|
|
|
2016-10-31 04:57:34 +03:00
|
|
|
"github.com/appleboy/drone-facebook/template"
|
2016-10-04 16:45:49 +03:00
|
|
|
"gopkg.in/telegram-bot-api.v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Repo information.
|
|
|
|
Repo struct {
|
|
|
|
Owner string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build information.
|
|
|
|
Build struct {
|
2016-10-31 04:57:34 +03:00
|
|
|
Event string
|
|
|
|
Number int
|
|
|
|
Commit string
|
|
|
|
Message string
|
|
|
|
Branch string
|
|
|
|
Author string
|
2016-11-04 04:38:21 +03:00
|
|
|
Email string
|
2016-10-31 04:57:34 +03:00
|
|
|
Status string
|
|
|
|
Link string
|
|
|
|
Started float64
|
|
|
|
Finished float64
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config for the plugin.
|
|
|
|
Config struct {
|
2016-11-09 05:35:55 +03:00
|
|
|
Token string
|
|
|
|
Debug bool
|
|
|
|
MatchEmail bool
|
|
|
|
To []string
|
|
|
|
Message []string
|
|
|
|
Photo []string
|
|
|
|
Document []string
|
|
|
|
Sticker []string
|
|
|
|
Audio []string
|
|
|
|
Voice []string
|
|
|
|
Location []string
|
|
|
|
Video []string
|
|
|
|
Venue []string
|
|
|
|
Format string
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin values.
|
|
|
|
Plugin struct {
|
|
|
|
Repo Repo
|
|
|
|
Build Build
|
|
|
|
Config Config
|
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
|
|
|
|
// Location format
|
|
|
|
Location struct {
|
2016-10-14 11:28:40 +03:00
|
|
|
Title string
|
|
|
|
Address string
|
2016-10-14 09:27:34 +03:00
|
|
|
Latitude float64
|
|
|
|
Longitude float64
|
|
|
|
}
|
2016-10-04 16:45:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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-14 09:27:34 +03:00
|
|
|
func convertLocation(value string) (Location, bool) {
|
|
|
|
var latitude, longitude float64
|
2016-10-14 11:28:40 +03:00
|
|
|
var title, address string
|
2016-10-14 09:27:34 +03:00
|
|
|
var err error
|
|
|
|
values := trimElement(strings.Split(value, ","))
|
|
|
|
|
|
|
|
if len(values) < 2 {
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:28:40 +03:00
|
|
|
if len(values) > 2 {
|
|
|
|
title = values[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) > 3 {
|
|
|
|
title = values[2]
|
|
|
|
address = values[3]
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:27:34 +03:00
|
|
|
latitude, err = strconv.ParseFloat(values[0], 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
longitude, err = strconv.ParseFloat(values[1], 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return Location{
|
2016-10-14 11:28:40 +03:00
|
|
|
Title: title,
|
|
|
|
Address: address,
|
2016-10-14 09:27:34 +03:00
|
|
|
Latitude: latitude,
|
|
|
|
Longitude: longitude,
|
|
|
|
}, false
|
|
|
|
}
|
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
|
|
|
var emails []int64
|
|
|
|
var ids []int64
|
2016-11-09 07:23:19 +03:00
|
|
|
attachEmail := true
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2016-11-17 08:13:43 +03:00
|
|
|
for _, value := range trimElement(to) {
|
2016-11-09 07:00:36 +03:00
|
|
|
idArray := trimElement(strings.Split(value, ":"))
|
|
|
|
|
|
|
|
// check id
|
|
|
|
id, err := strconv.ParseInt(idArray[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check match author email
|
|
|
|
if len(idArray) > 1 {
|
|
|
|
if email := idArray[1]; email != authorEmail {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
emails = append(emails, id)
|
2016-11-09 07:23:19 +03:00
|
|
|
attachEmail = false
|
2016-11-09 07:00:36 +03:00
|
|
|
continue
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-11-09 07:00:36 +03:00
|
|
|
|
|
|
|
ids = append(ids, id)
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:23:19 +03:00
|
|
|
if matchEmail == true && attachEmail == false {
|
2016-11-09 07:00:36 +03:00
|
|
|
return emails
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range emails {
|
|
|
|
ids = append(ids, value)
|
2016-11-04 04:38:21 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
return ids
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
ids := parseTo(p.Config.To, p.Build.Email, p.Config.MatchEmail)
|
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-14 08:36:08 +03:00
|
|
|
audios := fileExist(trimElement(p.Config.Audio))
|
2016-10-14 08:57:31 +03:00
|
|
|
voices := fileExist(trimElement(p.Config.Voice))
|
2016-10-14 10:36:50 +03:00
|
|
|
videos := fileExist(trimElement(p.Config.Video))
|
2016-10-14 09:27:34 +03:00
|
|
|
locations := trimElement(p.Config.Location)
|
2016-10-14 11:28:40 +03:00
|
|
|
venues := trimElement(p.Config.Venue)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
// send message.
|
2016-11-09 07:00:36 +03:00
|
|
|
for _, user := range ids {
|
2016-10-04 16:45:49 +03:00
|
|
|
for _, value := range trimElement(message) {
|
2016-10-31 04:57:34 +03:00
|
|
|
txt, err := template.RenderTrim(value, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(user, txt)
|
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-14 08:36:08 +03:00
|
|
|
|
|
|
|
for _, value := range audios {
|
|
|
|
msg := tgbotapi.NewAudioUpload(user, value)
|
|
|
|
msg.Title = "Audio Message."
|
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
2016-10-14 08:57:31 +03:00
|
|
|
|
|
|
|
for _, value := range voices {
|
|
|
|
msg := tgbotapi.NewVoiceUpload(user, value)
|
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
|
2016-10-14 10:36:50 +03:00
|
|
|
for _, value := range videos {
|
|
|
|
msg := tgbotapi.NewVideoUpload(user, value)
|
|
|
|
msg.Caption = "Video Message"
|
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:27:34 +03:00
|
|
|
for _, value := range locations {
|
|
|
|
location, empty := convertLocation(value)
|
|
|
|
|
|
|
|
if empty == true {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
|
|
|
|
p.Send(bot, msg)
|
|
|
|
}
|
2016-10-14 11:28:40 +03:00
|
|
|
|
|
|
|
for _, value := range venues {
|
|
|
|
location, empty := convertLocation(value)
|
|
|
|
|
|
|
|
if empty == true {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude)
|
|
|
|
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,
|
|
|
|
)}
|
|
|
|
}
|