add logic
This commit is contained in:
		
							parent
							
								
									c8fb28dcf6
								
							
						
					
					
						commit
						a3fcc634ef
					
				@ -1,3 +1,12 @@
 | 
			
		||||
# all_tg_bot
 | 
			
		||||
 | 
			
		||||
Бот подменяет @all на список участников чата
 | 
			
		||||
 | 
			
		||||
    Usage:
 | 
			
		||||
        main [OPTIONS]
 | 
			
		||||
    
 | 
			
		||||
    Application Options:
 | 
			
		||||
        -t, --token= Telegram api token
 | 
			
		||||
    
 | 
			
		||||
    Help Options:
 | 
			
		||||
        -h, --help   Show this help message
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								go.mod
									
									
									
									
									
								
							@ -1,3 +1,10 @@
 | 
			
		||||
module git.user-penguin.space/vladimir/all_tg_bot
 | 
			
		||||
 | 
			
		||||
go 1.18
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
 | 
			
		||||
	github.com/umputun/go-flags v1.5.1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require github.com/technoweenie/multipartstreamer v1.0.1 // indirect
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										60
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								main.go
									
									
									
									
									
								
							@ -1,7 +1,63 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
 | 
			
		||||
	"github.com/umputun/go-flags"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	tag      = "@all"
 | 
			
		||||
	allUsers = "@vo13crabs @disconecto @v9gel @KeyM_v2 @k_ukolov @iamembarrassed @freel_Fedorov_Kirill @butinmv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Opts struct {
 | 
			
		||||
	Token string `short:"t" long:"token" description:"Telegram api token"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var opts Opts
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("Hello, World!")
 | 
			
		||||
	run()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func run() {
 | 
			
		||||
	p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
 | 
			
		||||
	p.SubcommandsOptional = true
 | 
			
		||||
	if _, err := p.Parse(); err != nil {
 | 
			
		||||
		if err.(*flags.Error).Type != flags.ErrHelp {
 | 
			
		||||
			log.Println(errors.New("[ERROR] cli error: " + err.Error()))
 | 
			
		||||
		}
 | 
			
		||||
		os.Exit(2)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bot, err := tgbot.NewBotAPI(opts.Token)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	u := tgbot.NewUpdate(0)
 | 
			
		||||
	u.Timeout = 60
 | 
			
		||||
 | 
			
		||||
	updates, err := bot.GetUpdatesChan(u)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for update := range updates {
 | 
			
		||||
 | 
			
		||||
		if update.Message == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		text := update.Message.Text
 | 
			
		||||
		chatID := update.Message.Chat.ID
 | 
			
		||||
 | 
			
		||||
		if strings.Contains(strings.ToLower(text), tag) {
 | 
			
		||||
			_, _ = bot.Send(tgbot.NewMessage(chatID, allUsers))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user