all repos — escarbot @ 661438f9080f348c334f56cc65014c5288474afb

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	return &EscarBot{
47		Bot:            bot,
48		Power:          true,
49		LinkDetection:  true,
50		ChannelForward: true,
51		AdminForward:   true,
52		ChannelID:      channelIdInt,
53		GroupID:        groupIdInt,
54		AdminID:        adminIdInt,
55	}
56}
57
58func BotPoll(escarbot *EscarBot) {
59	u := tgbotapi.NewUpdate(0)
60	u.Timeout = 60
61
62	bot := escarbot.Bot
63	updates := bot.GetUpdatesChan(u)
64
65	for update := range updates {
66		msg := update.Message
67		if msg != nil { // If we got a message
68			if escarbot.LinkDetection {
69				handleLinks(bot, msg)
70			}
71			if escarbot.AdminForward {
72				forwardToAdmin(escarbot, msg)
73			}
74		}
75		if update.ChannelPost != nil { // If we got a channel post
76			if escarbot.ChannelForward {
77				channelPostHandler(escarbot, update.ChannelPost)
78			}
79		}
80	}
81}