Add LICENSE and first command
This commit may be used as template for making any new Telegram bots. Just check out it, change your variables and start developing new shiny bot! Maybe I need to create bot generator...
This commit is contained in:
@@ -12,20 +12,16 @@ import (
|
||||
var (
|
||||
c *context.Context
|
||||
log zerolog.Logger
|
||||
)
|
||||
|
||||
// Telegram is a struch which handles Telegram instance handling functions
|
||||
type Telegram struct {
|
||||
bot *telegram.Bot
|
||||
}
|
||||
)
|
||||
|
||||
// New initializes package
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
log = c.Logger.With().Str("domain", "telegram").Int("version", 1).Logger()
|
||||
t := &Telegram{}
|
||||
|
||||
log.Info().Msg("Starting Telegram instance")
|
||||
|
||||
t.StartBot()
|
||||
StartBot()
|
||||
}
|
||||
|
26
internal/telegram/respond.go
Normal file
26
internal/telegram/respond.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Fantasy World Zookeeper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
func getMessageParams(chatID int64, message string, disableWebPagePreview bool) telegram.SendMessageParameters {
|
||||
return telegram.SendMessageParameters{
|
||||
ChatID: chatID,
|
||||
Text: message,
|
||||
ParseMode: "Markdown",
|
||||
DisableWebPagePreview: disableWebPagePreview}
|
||||
}
|
||||
|
||||
// RespondWithMarkdown will send message to given chat with Markdown parse mode
|
||||
func RespondWithMarkdown(chatID int64, message string) {
|
||||
messageParams := getMessageParams(chatID, message, false)
|
||||
|
||||
_, err := bot.SendMessage(&messageParams)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
}
|
@@ -12,7 +12,7 @@ import (
|
||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/router"
|
||||
)
|
||||
|
||||
func (t *Telegram) proxyDialer(addr string) (net.Conn, error) {
|
||||
func proxyDialer(addr string) (net.Conn, error) {
|
||||
log.Debug().Msgf("Proxy used: %s", c.Config.Telegram.Proxy.Address)
|
||||
proxyAuth := proxy.Auth{}
|
||||
if c.Config.Telegram.Proxy.Username != "" {
|
||||
@@ -30,24 +30,24 @@ func (t *Telegram) proxyDialer(addr string) (net.Conn, error) {
|
||||
}
|
||||
|
||||
// Bot returns Telegram instance
|
||||
func (t *Telegram) Bot() *telegram.Bot {
|
||||
return t.bot
|
||||
func Bot() *telegram.Bot {
|
||||
return bot
|
||||
}
|
||||
|
||||
// StartBot starts connection with Telegram
|
||||
func (t *Telegram) StartBot() {
|
||||
func StartBot() {
|
||||
// Any errors here considered fatal, because main purpose of this app is Telegram interactions
|
||||
var err error
|
||||
var updates telegram.UpdatesChannel
|
||||
if c.Config.Telegram.Proxy.Enabled {
|
||||
t.bot = new(telegram.Bot)
|
||||
bot = new(telegram.Bot)
|
||||
client := new(http.Client)
|
||||
client.Dial = t.proxyDialer
|
||||
t.bot.SetClient(client)
|
||||
t.bot.AccessToken = c.Config.Telegram.Token
|
||||
t.bot.User, err = t.bot.GetMe()
|
||||
client.Dial = proxyDialer
|
||||
bot.SetClient(client)
|
||||
bot.AccessToken = c.Config.Telegram.Token
|
||||
bot.User, err = bot.GetMe()
|
||||
} else {
|
||||
t.bot, err = telegram.New(c.Config.Telegram.Token)
|
||||
bot, err = telegram.New(c.Config.Telegram.Token)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
@@ -59,22 +59,22 @@ func (t *Telegram) StartBot() {
|
||||
if len(url.Host()) == 0 {
|
||||
log.Fatal().Msg("Can't parse webhook URL: got empty host")
|
||||
}
|
||||
log.Info().Msg("Trying to set webhook: " + url.String() + t.bot.AccessToken)
|
||||
log.Info().Msg("Trying to set webhook: " + url.String() + bot.AccessToken)
|
||||
|
||||
webhook := telegram.NewWebhook(url.String()+t.bot.AccessToken, nil)
|
||||
webhook := telegram.NewWebhook(url.String()+bot.AccessToken, nil)
|
||||
webhook.MaxConnections = 40
|
||||
|
||||
updates = t.bot.NewWebhookChannel(url, webhook, "", "", c.Config.Telegram.Webhook.Listen)
|
||||
updates = bot.NewWebhookChannel(url, webhook, "", "", c.Config.Telegram.Webhook.Listen)
|
||||
} else {
|
||||
log.Warn().Msg("Using long-polling for updates (not recommended)")
|
||||
var info *telegram.WebhookInfo
|
||||
info, err = t.bot.GetWebhookInfo()
|
||||
info, err = bot.GetWebhookInfo()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
if info != nil && info.URL != "" {
|
||||
log.Info().Msg("Deleting old webhook...")
|
||||
_, err := t.bot.DeleteWebhook()
|
||||
_, err := bot.DeleteWebhook()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
@@ -84,14 +84,13 @@ func (t *Telegram) StartBot() {
|
||||
Limit: 100,
|
||||
Timeout: 60,
|
||||
}
|
||||
updates = t.bot.NewLongPollingChannel(&updatesParams)
|
||||
updates = bot.NewLongPollingChannel(&updatesParams)
|
||||
}
|
||||
|
||||
log.Info().Msg("Connection with Telegram established")
|
||||
|
||||
for update := range updates {
|
||||
log.Debug().Msgf("%+v", update)
|
||||
go router.Requests.Respond(update)
|
||||
go router.Respond(update)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user