Adding telegram bot

This commit is contained in:
Andreas Mieke 2020-03-26 18:11:12 +01:00
parent f2617cd6a7
commit 622a3d109e
6 changed files with 102 additions and 10 deletions

View file

@ -6,8 +6,8 @@ import (
"os"
"os/signal"
"git.1750studios.com/ToddShepard/DB640/internal/bot"
"git.1750studios.com/ToddShepard/DB640/internal/config"
"git.1750studios.com/ToddShepard/DB640/internal/twitterbot"
)
func main() {
@ -18,12 +18,12 @@ func main() {
}
config.LoadConfig(*cfg)
twitterbot.Init()
bot.Init()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
twitterbot.DeInit()
bot.DeInit()
config.WriteConfig(*cfg)
}

2
go.mod
View file

@ -6,5 +6,7 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f
github.com/dghubble/oauth1 v0.6.0
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/jinzhu/gorm v1.9.12
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
)

5
go.sum
View file

@ -16,6 +16,9 @@ github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DP
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-telegram-bot-api/telegram-bot-api v1.0.0 h1:HXVtsZ+yINQeyyhPFAUU4yKmeN+iFhJ87jXZOC016gs=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@ -36,6 +39,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM=

View file

@ -1,4 +1,4 @@
package twitterbot
package bot
import (
"log"
@ -6,11 +6,13 @@ import (
"git.1750studios.com/ToddShepard/DB640/internal/config"
"git.1750studios.com/ToddShepard/DB640/internal/database"
"git.1750studios.com/ToddShepard/DB640/internal/telegram"
"git.1750studios.com/ToddShepard/DB640/internal/twitter"
"github.com/jinzhu/gorm"
)
var stream *twitter.Stream
var updates telegram.UpdateChan
// Init initzializes the bot and subscribes the magic hashtag feed
func Init() {
@ -18,16 +20,24 @@ func Init() {
err = database.Open(config.C.Database.Dialect, config.C.Database.Connection)
if err != nil {
log.Fatalf("could not establish database connection: %+v", err)
log.Fatalf("Could not establish database connection: %+v", err)
}
log.Printf("[DATABASE] Connection established\n")
twitter.Init()
stream, err = twitter.GetStreamForTag(config.C.Twitter.MagicHashtag)
if err != nil {
log.Fatalf("Could not establish twitter stream: %+v", err)
}
go twitter.StreamDemux(stream, handleHashtagTweet)
log.Printf("[TWITTER] Connection established\n")
twitter.StreamDemux(stream, handleHashtagTweet)
updates, err = telegram.Init()
if err != nil {
log.Fatalf("Could not establish telegram connection: %+v", err)
}
go handleTelegram()
log.Printf("[TELEGRAM] Connection established\n")
}
// DeInit stops the stream and deinitzializes the bot
@ -40,20 +50,20 @@ func handleHashtagTweet(tweet *twitter.Tweet) {
msg, tags := twitter.GetTextAndHashtags(tweet)
// Ignore retweets
if tweet.RetweetedStatus != nil {
log.Printf("%s: %s - IGNORED (RT)\n", tweet.User.ScreenName, msg)
log.Printf("[TWITTER] %s: %s - IGNORED (RT)\n", tweet.User.ScreenName, msg)
return
}
// Ignore replys
if tweet.InReplyToStatusID != 0 {
log.Printf("%s: %s - IGNORED (RPLY)\n", tweet.User.ScreenName, msg)
log.Printf("[TWITTER] %s: %s - IGNORED (RPLY)\n", tweet.User.ScreenName, msg)
return
}
// Ignore if only magic hashtag is given
if len(tags) < 2 {
log.Printf("%s: %s - IGNORED (<2#)\n", tweet.User.ScreenName, msg)
log.Printf("[TWITTER] %s: %s - IGNORED (<2#)\n", tweet.User.ScreenName, msg)
return
}
log.Printf("%s: %s\n", tweet.User.ScreenName, msg)
log.Printf("[TWITTER] %s: %s\n", tweet.User.ScreenName, msg)
go findCodes(tweet)
}
@ -81,3 +91,30 @@ func sendReply(tweet *twitter.Tweet, betriebsstellen []database.Betriebsstelle)
log.Printf("Cannot send reply, error: %+v", err)
}
}
func handleTelegram() {
for update := range updates {
if update.Message == nil {
// ignore any non-Message Updates
continue
}
log.Printf("[TELEGRAM] %s: %s\n", update.Message.From.UserName, update.Message.Text)
if update.Message.Text == "/start" || update.Message.Text == "/help" {
reply := "Willkommen beim DB 640 Telegram Bot!\n\nEinfach den gewünschten DB 640 Betriebsstellencode schicken, und der Bot antwortet mit der zugehörigen Betriebsstelle!\n\nZum Beispiel: Nb -> Wiener Neustadt Hbf (in Nb)"
telegram.SendReply(reply, update)
continue
}
var bs database.Betriebsstelle
var reply string
if database.Db.First(&bs, "code = ?", update.Message.Text).Error != gorm.ErrRecordNotFound {
reply = bs.Code + ": " + bs.Name
} else {
reply = update.Message.Text + ": nicht gefunden!"
}
err := telegram.SendReply(reply, update)
if err != nil {
log.Printf("Cannot send reply, error: %+v", err)
}
}
}

View file

@ -13,6 +13,7 @@ import (
// Config main type
type Config struct {
Twitter Twitter
Telegram Telegram
Database Database
}
@ -25,6 +26,11 @@ type Twitter struct {
MagicHashtag string
}
// Telegram related config
type Telegram struct {
APIKey string
}
// Database related config
type Database struct {
Dialect string
@ -42,6 +48,8 @@ func LoadDefaults() {
C.Twitter.ConsumerSecret = "CONSUMERSECRET"
C.Twitter.MagicHashtag = "#DB640"
C.Telegram.APIKey = "APIKEY"
C.Database.Dialect = "sqlite3"
C.Database.Connection = ":memory:"
}

View file

@ -0,0 +1,40 @@
package telegram
import (
"log"
"git.1750studios.com/ToddShepard/DB640/internal/config"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
// UpdateChan is telegram UpdatesChannel
type UpdateChan = tgbotapi.UpdatesChannel
var bot *tgbotapi.BotAPI
// Init initzializes the telegram bot
func Init() (UpdateChan, error) {
var err error
bot, err = tgbotapi.NewBotAPI(config.C.Telegram.APIKey)
if err != nil {
return nil, err
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
return bot.GetUpdatesChan(u)
}
// SendReply sends a reply to the given update
func SendReply(reply string, update tgbotapi.Update) error {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
//msg.ReplyToMessageID = update.Message.MessageID
if _, err := bot.Send(msg); err != nil {
return err
}
return nil
}