2020-03-26 17:11:12 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.1750studios.com/ToddShepard/DB640/internal/config"
|
2020-04-02 16:17:19 +00:00
|
|
|
"git.1750studios.com/ToddShepard/DB640/internal/database"
|
2020-03-26 17:11:12 +00:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
2020-04-02 16:17:19 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-04-02 17:49:41 +00:00
|
|
|
uuid "github.com/nu7hatch/gouuid"
|
2020-03-26 17:11:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateChan is telegram UpdatesChannel
|
|
|
|
type UpdateChan = tgbotapi.UpdatesChannel
|
|
|
|
|
|
|
|
var bot *tgbotapi.BotAPI
|
|
|
|
|
|
|
|
// Init initzializes the telegram bot
|
2020-04-02 16:17:19 +00:00
|
|
|
func Init() (err error) {
|
2020-03-26 17:11:12 +00:00
|
|
|
bot, err = tgbotapi.NewBotAPI(config.C.Telegram.APIKey)
|
|
|
|
if err != nil {
|
2020-04-02 16:17:19 +00:00
|
|
|
return
|
2020-03-26 17:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
2020-04-02 16:17:19 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-26 17:11:12 +00:00
|
|
|
|
2020-04-02 16:17:19 +00:00
|
|
|
// GetChan returns the Telegram update channel
|
|
|
|
func GetChan() (UpdateChan, error) {
|
2020-03-26 17:11:12 +00:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
|
|
|
|
return bot.GetUpdatesChan(u)
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:17:19 +00:00
|
|
|
// DeInit deinitzialisizes the bot
|
|
|
|
func DeInit() {
|
|
|
|
bot.StopReceivingUpdates()
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:11:12 +00:00
|
|
|
// SendReply sends a reply to the given update
|
|
|
|
func SendReply(reply string, update tgbotapi.Update) error {
|
2020-04-02 16:17:19 +00:00
|
|
|
var chat database.TGChat
|
|
|
|
chat.ID = update.Message.Chat.ID
|
|
|
|
|
|
|
|
if database.Db.First(&chat).Error != gorm.ErrRecordNotFound {
|
|
|
|
chat.FirstName = update.Message.Chat.FirstName
|
|
|
|
chat.LastName = update.Message.Chat.LastName
|
|
|
|
chat.UserName = update.Message.Chat.UserName
|
|
|
|
database.Db.Save(&chat)
|
|
|
|
} else {
|
|
|
|
chat.FirstName = update.Message.Chat.FirstName
|
|
|
|
chat.LastName = update.Message.Chat.LastName
|
|
|
|
chat.UserName = update.Message.Chat.UserName
|
|
|
|
database.Db.Create(&chat)
|
|
|
|
}
|
2020-03-26 17:11:12 +00:00
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
|
|
|
|
|
|
|
|
if _, err := bot.Send(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-02 16:17:19 +00:00
|
|
|
|
|
|
|
// SendAll sends a message to all active users
|
|
|
|
func SendAll(msg string) {
|
|
|
|
log.Printf("[TELEGRAM] Sending to all chats: %s", msg)
|
|
|
|
var chats []database.TGChat
|
|
|
|
database.Db.Find(&chats)
|
|
|
|
for _, chat := range chats {
|
|
|
|
tgmsg := tgbotapi.NewMessage(chat.ID, msg)
|
|
|
|
if _, err := bot.Send(tgmsg); err != nil && err.Error() == "Forbidden: bot was blocked by the user" {
|
|
|
|
database.Db.Unscoped().Delete(&chat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 17:49:41 +00:00
|
|
|
|
|
|
|
// DoInlineQuery does the inline query search and returns results
|
|
|
|
func DoInlineQuery(update tgbotapi.Update) {
|
|
|
|
var config tgbotapi.InlineConfig
|
|
|
|
var results []interface{}
|
|
|
|
var betriebsstellen []database.Betriebsstelle
|
|
|
|
|
|
|
|
config.InlineQueryID = update.InlineQuery.ID
|
|
|
|
config.Results = results
|
|
|
|
|
|
|
|
err := database.Db.Limit(50).Find(&betriebsstellen, "Code LIKE ?", update.InlineQuery.Query+"%").Error
|
|
|
|
if err != nil || len(betriebsstellen) == 0 {
|
|
|
|
bot.AnswerInlineQuery(config)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, bs := range betriebsstellen {
|
|
|
|
msg := bs.Code + ": " + bs.Name
|
|
|
|
id, _ := uuid.NewV4()
|
|
|
|
results = append(results, tgbotapi.NewInlineQueryResultArticle(id.String(), msg, msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Results = results
|
|
|
|
bot.AnswerInlineQuery(config)
|
|
|
|
log.Printf("[TELEGRAM Inline] %s: %s", update.InlineQuery.From.String(), update.InlineQuery.Query)
|
|
|
|
}
|