Skip to content
Snippets Groups Projects
Select Git revision
  • 8f000d6628c5fcc74912010b89330e56fc6c8356
  • master default protected
2 results

telegram.go

Blame
  • telegram.go 11.90 KiB
    package main
    
    import (
    	"fmt"
    	"github.com/bwmarrin/discordgo"
    	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
    	"log"
    	"net/http"
    	"strconv"
    	"strings"
    )
    
    var botAPI *tgbotapi.BotAPI
    
    type botLogger struct{}
    
    func (botLogger) Printf(format string, v ...interface{}) {
    	log.Printf("telegram: "+format, v...)
    }
    func (botLogger) Println(v ...interface{}) {
    	log.Println(append([]interface{}{"telegram:"}, v...)...)
    }
    
    func init() {
    	if err := tgbotapi.SetLogger(botLogger{}); err != nil {
    		panic(err)
    	}
    }
    
    func openTelegram() {
    	if bot, err := tgbotapi.NewBotAPI(config.Telegram.Token); err != nil {
    		log.Fatalf("error connecting to telegram: %s", err)
    	} else {
    		botAPI = bot
    	}
    	log.Printf("connected to telegram as @%s (%v)", botAPI.Self.UserName, botAPI.Self.ID)
    	botAPI.Debug = config.System.Verbose
    
    	ready <- struct{}{}
    }
    
    func handleTelegram() {
    	u := tgbotapi.NewUpdate(0)
    	u.Timeout = 60
    
    	if updates, err := botAPI.GetUpdatesChan(u); err != nil {
    		log.Fatalf("error getting updates: %s", err)
    	} else {
    		if config.Telegram.BypassBacklog {
    			for update := range updates {
    				if update.Message == nil {
    					continue
    				}
    
    				if update.Message.Time().After(readyTime) {
    					if config.System.Verbose {
    						log.Printf("update %v after current, breaking bypass loop", update.UpdateID)
    					}
    					respondTelegram(update)
    					break
    				}
    
    				if config.System.Verbose {
    					log.Printf("skipped update %v", update.UpdateID)
    				}
    			}
    		}
    
    		for update := range updates {
    			respondTelegram(update)