all repos — escarbot @ 07d968fe3a53370818ff1b969eff3271d96d718c

Earthbound Café's custom Telegram bot, with lots of cool utilities built-in.

telegram/telegram.go (view raw)

 1package telegram
 2
 3import (
 4	"log"
 5	"strconv"
 6
 7	tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5"
 8)
 9
10type EscarBot struct {
11	Bot            *tgbotapi.BotAPI
12	Power          bool
13	LinkDetection  bool
14	ChannelForward bool
15	AdminForward   bool
16	ChannelID      int64
17	GroupID        int64
18	AdminID        int64
19}
20
21func NewBot(botToken string, channelId string, groupId string, adminId string) *EscarBot {
22	bot, err := tgbotapi.NewBotAPI(botToken)
23	if err != nil {
24		log.Panic(err)
25	}
26
27	//bot.Debug = true
28
29	log.Printf("Authorized on account %s", bot.Self.UserName)
30
31	channelIdInt, err := strconv.ParseInt(channelId, 10, 64)
32	if err != nil {
33		log.Fatal("Error while converting CHANNEL_ID to int64:", err)
34	}
35
36	groupIdInt, err := strconv.ParseInt(groupId, 10, 64)
37	if err != nil {
38		log.Fatal("Error while converting GROUP_ID to int64:", err)
39	}
40
41	adminIdInt, err := strconv.ParseInt(adminId, 10, 64)
42	if err != nil {
43		log.Fatal("Error while converting ADMIN_ID to int64:", err)
44	}
45
46	emptyKeyboard = tgbotapi.NewInlineKeyboardMarkup()
47	emptyKeyboard.InlineKeyboard = [][]tgbotapi.InlineKeyboardButton{}
48
49	return &EscarBot{
50		Bot:            bot,
51		Power:          true,
52		LinkDetection:  true,
53		ChannelForward: true,
54		AdminForward:   true,
55		ChannelID:      channelIdInt,
56		GroupID:        groupIdInt,
57		AdminID:        adminIdInt,
58	}
59}
60
61func BotPoll(escarbot *EscarBot) {
62	u := tgbotapi.NewUpdate(0)
63	u.Timeout = 60
64
65	bot := escarbot.Bot
66	updates := bot.GetUpdatesChan(u)
67
68	for update := range updates {
69		msg := update.Message
70		if msg != nil { // If we got a message
71			if escarbot.LinkDetection {
72				handleLinks(bot, msg)
73			}
74			if escarbot.AdminForward {
75				forwardToAdmin(escarbot, msg)
76			}
77		}
78		if update.ChannelPost != nil { // If we got a channel post
79			if escarbot.ChannelForward {
80				channelPostHandler(escarbot, update.ChannelPost)
81			}
82		}
83		query := update.CallbackQuery
84		if query != nil { // If we got a callback query
85			callbackQueryHandler(bot, query)
86		}
87	}
88}