This commit is contained in:
Akulij 2025-03-20 03:06:10 +07:00
parent ccc98b4298
commit 14afe8ff4d

View File

@ -1,14 +1,14 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io"
"log" "log"
"strings" "net/http"
"os"
"strconv" "strconv"
"errors" "strings"
"net/http"
"os"
"io"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -19,359 +19,359 @@ import (
) )
type User struct { type User struct {
gorm.Model gorm.Model
ID int64 ID int64
State string State string
MsgCounter uint MsgCounter uint
RoleBitmask uint RoleBitmask uint
} }
func (u User) IsAdmin() bool { func (u User) IsAdmin() bool {
return u.RoleBitmask & 1 == 1 return u.RoleBitmask&1 == 1
} }
func (u User) IsEffectiveAdmin() bool { func (u User) IsEffectiveAdmin() bool {
return u.RoleBitmask & 0b10 == 0b10 return u.RoleBitmask&0b10 == 0b10
} }
type BotContent struct { type BotContent struct {
gorm.Model gorm.Model
Literal string Literal string
Content string Content string
} }
type BotController struct { type BotController struct {
cfg config.Config cfg config.Config
bot *tgbotapi.BotAPI bot *tgbotapi.BotAPI
db *gorm.DB db *gorm.DB
updates tgbotapi.UpdatesChannel updates tgbotapi.UpdatesChannel
} }
func DBMigrate() (*gorm.DB, error) { func DBMigrate() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil { if err != nil {
return db, err return db, err
} }
db.AutoMigrate(&User{}) db.AutoMigrate(&User{})
db.AutoMigrate(&BotContent{}) db.AutoMigrate(&BotContent{})
return db, err return db, err
} }
func GetBotController() BotController { func GetBotController() BotController {
cfg := config.GetConfig() cfg := config.GetConfig()
fmt.Printf("Token value: '%v'\n", cfg.BotToken) fmt.Printf("Token value: '%v'\n", cfg.BotToken)
fmt.Printf("Admin password: '%v'\n", cfg.AdminPass) fmt.Printf("Admin password: '%v'\n", cfg.AdminPass)
bot, err := tgbotapi.NewBotAPI(cfg.BotToken) bot, err := tgbotapi.NewBotAPI(cfg.BotToken)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
db, err := DBMigrate() db, err := DBMigrate()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
bot.Debug = true bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName) log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0) u := tgbotapi.NewUpdate(0)
u.Timeout = 60 u.Timeout = 60
updates := bot.GetUpdatesChan(u) updates := bot.GetUpdatesChan(u)
return BotController{cfg: cfg, bot: bot, db: db, updates: updates} return BotController{cfg: cfg, bot: bot, db: db, updates: updates}
} }
func (bc BotController) GetBotContentVerbose(Literal string) (string, error) { func (bc BotController) GetBotContentVerbose(Literal string) (string, error) {
var c BotContent var c BotContent
bc.db.First(&c, "Literal", Literal) bc.db.First(&c, "Literal", Literal)
if c == (BotContent{}) { if c == (BotContent{}) {
return "[Unitialized] Init in Admin panel! Literal: " + Literal, errors.New("No content") return "[Unitialized] Init in Admin panel! Literal: " + Literal, errors.New("No content")
} }
return c.Content, nil return c.Content, nil
} }
func (bc BotController) GetBotContent(Literal string) string { func (bc BotController) GetBotContent(Literal string) string {
content, _ := bc.GetBotContentVerbose(Literal) content, _ := bc.GetBotContentVerbose(Literal)
return content return content
} }
func (bc BotController) SetBotContent(Literal string, Content string) { func (bc BotController) SetBotContent(Literal string, Content string) {
c := BotContent{Literal: Literal, Content: Content} c := BotContent{Literal: Literal, Content: Content}
bc.db.FirstOrCreate(&c, "Literal", Literal) bc.db.FirstOrCreate(&c, "Literal", Literal)
bc.db.Model(&c).Update("Content", Content) bc.db.Model(&c).Update("Content", Content)
} }
func main() { func main() {
var bc = GetBotController() var bc = GetBotController()
for update := range bc.updates { for update := range bc.updates {
go ProcessUpdate(bc, update) go ProcessUpdate(bc, update)
} }
} }
func ProcessUpdate(bc BotController, update tgbotapi.Update) { func ProcessUpdate(bc BotController, update tgbotapi.Update) {
if update.Message != nil { if update.Message != nil {
var UserID = update.Message.From.ID var UserID = update.Message.From.ID
var user User var user User
bc.db.First(&user, "id", UserID) bc.db.First(&user, "id", UserID)
if user == (User{}) { if user == (User{}) {
log.Printf("New user: [%d]", UserID) log.Printf("New user: [%d]", UserID)
user = User{ID: UserID , State: "start"} user = User{ID: UserID, State: "start"}
bc.db.Create(&user) bc.db.Create(&user)
} }
bc.db.Model(&user).Update("MsgCounter", user.MsgCounter + 1) bc.db.Model(&user).Update("MsgCounter", user.MsgCounter+1)
log.Printf("User[%d] messages: %d", user.ID, user.MsgCounter) log.Printf("User[%d] messages: %d", user.ID, user.MsgCounter)
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
possibleCommand := strings.Split(update.Message.Text, " ")[0] possibleCommand := strings.Split(update.Message.Text, " ")[0]
args := strings.Split(update.Message.Text, " ")[1:] args := strings.Split(update.Message.Text, " ")[1:]
log.Printf("Args: %s", args) log.Printf("Args: %s", args)
if possibleCommand == "/start" { if possibleCommand == "/start" {
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
kbd := tgbotapi.NewInlineKeyboardMarkup( kbd := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"), tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"),
), ),
) )
if user.IsAdmin() { if user.IsAdmin() {
kbd = tgbotapi.NewInlineKeyboardMarkup( kbd = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"), tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"),
), ),
tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Panel", "panel"), tgbotapi.NewInlineKeyboardButtonData("Panel", "panel"),
), ),
) )
} }
img, err := bc.GetBotContentVerbose("preview_image") img, err := bc.GetBotContentVerbose("preview_image")
if err != nil || img == "" { if err != nil || img == "" {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, bc.GetBotContent("start")) msg := tgbotapi.NewMessage(update.Message.Chat.ID, bc.GetBotContent("start"))
// msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hello, [user](tg://user?id=958170391)") // msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hello, [user](tg://user?id=958170391)")
msg.ParseMode = "markdown" msg.ParseMode = "markdown"
msg.ReplyMarkup = kbd msg.ReplyMarkup = kbd
bc.bot.Send(msg) bc.bot.Send(msg)
} else { } else {
msg := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FileID(img)) msg := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FileID(img))
msg.Caption = bc.GetBotContent("start") msg.Caption = bc.GetBotContent("start")
msg.ReplyMarkup = kbd msg.ReplyMarkup = kbd
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} else if possibleCommand == "/id" && user.IsAdmin() { } else if possibleCommand == "/id" && user.IsAdmin() {
log.Printf("THERe") log.Printf("THERe")
bc.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatInt(update.Message.Chat.ID, 10))) bc.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatInt(update.Message.Chat.ID, 10)))
} else if possibleCommand == "/secret" && len(args) > 0 && args[0] == bc.cfg.AdminPass { } else if possibleCommand == "/secret" && len(args) > 0 && args[0] == bc.cfg.AdminPass {
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b11) // set real admin ID (0b1) and effective admin toggle (0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b11) // set real admin ID (0b1) and effective admin toggle (0b10)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!")
bc.bot.Send(msg) bc.bot.Send(msg)
} else if possibleCommand == "/panel" && user.IsAdmin() { } else if possibleCommand == "/panel" && user.IsAdmin() {
if !user.IsEffectiveAdmin() { if !user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b10)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You was in usermode, turned back to admin mode...") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You was in usermode, turned back to admin mode...")
bc.bot.Send(msg) bc.bot.Send(msg)
} }
kbd := tgbotapi.NewInlineKeyboardMarkup( kbd := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Стартовая картинка", "update:preview_image")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Стартовая картинка", "update:preview_image")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Приветственный текст", "update:start")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Приветственный текст", "update:start")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Кнопка для заявки", "update:leave_ticket_button")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Кнопка для заявки", "update:leave_ticket_button")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID чата", "update:supportchatid")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID чата", "update:supportchatid")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID канала", "update:channelid")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID канала", "update:channelid")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Уведомление об отправке тикета", "update:sended_notify")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Уведомление об отправке тикета", "update:sended_notify")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба оставить тикет", "update:leaveticket_message")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба оставить тикет", "update:leaveticket_message")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба подписаться на канал", "update:subscribe_message")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба подписаться на канал", "update:subscribe_message")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Ссылка на канал", "update:channel_link")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Ссылка на канал", "update:channel_link")),
) )
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Выберите пункт для изменения") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Выберите пункт для изменения")
msg.ReplyMarkup = kbd msg.ReplyMarkup = kbd
bc.bot.Send(msg) bc.bot.Send(msg)
} else if possibleCommand == "/usermode" && user.IsEffectiveAdmin() { } else if possibleCommand == "/usermode" && user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask & (^uint(0b10))) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask&(^uint(0b10)))
log.Printf("Set role bitmask (%b) for user: %d", user.RoleBitmask, user.ID) log.Printf("Set role bitmask (%b) for user: %d", user.RoleBitmask, user.ID)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Simulating user experience!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Simulating user experience!")
bc.bot.Send(msg) bc.bot.Send(msg)
} else if user.IsEffectiveAdmin() && user.State != "leaveticket" { } else if user.IsEffectiveAdmin() && user.State != "leaveticket" {
if user.State != "start" { if user.State != "start" {
if strings.HasPrefix(user.State, "imgset:") { if strings.HasPrefix(user.State, "imgset:") {
Literal := strings.Split(user.State, ":")[1] Literal := strings.Split(user.State, ":")[1]
if update.Message.Text == "unset" { if update.Message.Text == "unset" {
var l BotContent var l BotContent
bc.db.First(&l, "Literal", Literal) bc.db.First(&l, "Literal", Literal)
bc.SetBotContent(Literal, "") bc.SetBotContent(Literal, "")
} }
maxsize := 0 maxsize := 0
fileid := "" fileid := ""
for _, p := range update.Message.Photo { for _, p := range update.Message.Photo {
if p.FileSize > maxsize { if p.FileSize > maxsize {
fileid = p.FileID fileid = p.FileID
maxsize = p.FileSize maxsize = p.FileSize
} }
} }
bc.SetBotContent(Literal, fileid) bc.SetBotContent(Literal, fileid)
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Succesfully set new image!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Succesfully set new image!")
bc.bot.Send(msg) bc.bot.Send(msg)
} else if strings.HasPrefix(user.State, "stringset:") { } else if strings.HasPrefix(user.State, "stringset:") {
Literal := strings.Split(user.State, ":")[1] Literal := strings.Split(user.State, ":")[1]
bc.SetBotContent(Literal, update.Message.Text) bc.SetBotContent(Literal, update.Message.Text)
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Succesfully set new text!") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Succesfully set new text!")
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} }
} else { } else {
if user.State == "leaveticket" { if user.State == "leaveticket" {
f := update.Message.From f := update.Message.From
ticket := fmt.Sprintf("User: %s %s\nUsername: %s\nText:\n", ticket := fmt.Sprintf("User: %s %s\nUsername: %s\nText:\n",
f.FirstName, f.LastName, f.FirstName, f.LastName,
f.UserName) f.UserName)
// Offset := len(ticket) // Offset := len(ticket)
// Length := len(update.Message.Text) // Length := len(update.Message.Text)
ticket += update.Message.Text ticket += update.Message.Text
chatidstr, err := bc.GetBotContentVerbose("supportchatid") chatidstr, err := bc.GetBotContentVerbose("supportchatid")
if err != nil { if err != nil {
var admins []User var admins []User
bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins) bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins)
for _, admin := range admins { for _, admin := range admins {
msg := tgbotapi.NewMessage(admin.ID, "Support ChatID is not set!!!") msg := tgbotapi.NewMessage(admin.ID, "Support ChatID is not set!!!")
msg.Entities = []tgbotapi.MessageEntity{tgbotapi.MessageEntity{ msg.Entities = []tgbotapi.MessageEntity{tgbotapi.MessageEntity{
Type: "code", Type: "code",
Offset: 1, Offset: 1,
Length: 2, Length: 2,
}} }}
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} }
chatid, _ := strconv.ParseInt(chatidstr, 10, 64) chatid, _ := strconv.ParseInt(chatidstr, 10, 64)
_, err = bc.bot.Send(tgbotapi.NewMessage(chatid, ticket)) _, err = bc.bot.Send(tgbotapi.NewMessage(chatid, ticket))
if err != nil { if err != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Something went wrong, try again...") msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Something went wrong, try again...")
bc.bot.Send(msg) bc.bot.Send(msg)
return return
} }
bc.db.Model(&user).Update("state", "start") bc.db.Model(&user).Update("state", "start")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, bc.GetBotContent("sended_notify")) msg := tgbotapi.NewMessage(update.Message.Chat.ID, bc.GetBotContent("sended_notify"))
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} }
} else if update.CallbackQuery != nil { } else if update.CallbackQuery != nil {
var user User var user User
bc.db.First(&user, "id", update.CallbackQuery.From.ID) bc.db.First(&user, "id", update.CallbackQuery.From.ID)
if update.CallbackQuery.Data == "leave_ticket_button" { if update.CallbackQuery.Data == "leave_ticket_button" {
chatidstr, err := bc.GetBotContentVerbose("channelid") chatidstr, err := bc.GetBotContentVerbose("channelid")
if err != nil { if err != nil {
var admins []User var admins []User
bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins) bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins)
for _, admin := range admins { for _, admin := range admins {
bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is not set!!!")) bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is not set!!!"))
} }
} }
chatid, _ := strconv.ParseInt(chatidstr, 10, 64) chatid, _ := strconv.ParseInt(chatidstr, 10, 64)
member, err := bc.bot.GetChatMember(tgbotapi.GetChatMemberConfig{ member, err := bc.bot.GetChatMember(tgbotapi.GetChatMemberConfig{
ChatConfigWithUser: tgbotapi.ChatConfigWithUser{ ChatConfigWithUser: tgbotapi.ChatConfigWithUser{
UserID: update.CallbackQuery.From.ID, UserID: update.CallbackQuery.From.ID,
SuperGroupUsername: chatidstr, SuperGroupUsername: chatidstr,
ChatID: chatid, ChatID: chatid,
}, },
}) })
if err != nil { if err != nil {
if strings.Contains(err.Error(), "chat not found") { if strings.Contains(err.Error(), "chat not found") {
bc.bot.Send(tgbotapi.NewMessage(user.ID, "No channel ID is set!!!")) bc.bot.Send(tgbotapi.NewMessage(user.ID, "No channel ID is set!!!"))
} }
} }
log.Printf("M: %s, E: %s", member, err) log.Printf("M: %s, E: %s", member, err)
s := member.Status s := member.Status
if s == "member" || s == "creator" || s == "admin" { if s == "member" || s == "creator" || s == "admin" {
bc.db.Model(&user).Update("state", "leaveticket") bc.db.Model(&user).Update("state", "leaveticket")
bc.bot.Send(tgbotapi.NewMessage(user.ID, bc.GetBotContent("leaveticket_message"))) bc.bot.Send(tgbotapi.NewMessage(user.ID, bc.GetBotContent("leaveticket_message")))
} else { } else {
link, err := bc.GetBotContentVerbose("channel_link") link, err := bc.GetBotContentVerbose("channel_link")
msg := tgbotapi.NewMessage(user.ID, bc.GetBotContent("subscribe_message")) msg := tgbotapi.NewMessage(user.ID, bc.GetBotContent("subscribe_message"))
if err == nil { if err == nil {
kbd := tgbotapi.NewInlineKeyboardMarkup( kbd := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonURL("Канал", link)), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonURL("Канал", link)),
) )
msg.ReplyMarkup = kbd msg.ReplyMarkup = kbd
} }
if err != nil { if err != nil {
log.Printf("NO LINK!!!") log.Printf("NO LINK!!!")
var admins []User var admins []User
bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins) bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins)
for _, admin := range admins { for _, admin := range admins {
msg := tgbotapi.NewMessage(admin.ID, "Channel link is not set!!!") msg := tgbotapi.NewMessage(admin.ID, "Channel link is not set!!!")
msg.Entities = []tgbotapi.MessageEntity{tgbotapi.MessageEntity{ msg.Entities = []tgbotapi.MessageEntity{tgbotapi.MessageEntity{
Type: "code", Type: "code",
Offset: 1, Offset: 1,
Length: 2, Length: 2,
}} }}
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} }
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} else if user.IsEffectiveAdmin() { } else if user.IsEffectiveAdmin() {
if strings.HasPrefix(update.CallbackQuery.Data, "update:") { if strings.HasPrefix(update.CallbackQuery.Data, "update:") {
Label := strings.Split(update.CallbackQuery.Data, ":")[1] Label := strings.Split(update.CallbackQuery.Data, ":")[1]
if Label == "preview_image" { if Label == "preview_image" {
bc.db.Model(&user).Update("state", "imgset:" + Label) bc.db.Model(&user).Update("state", "imgset:"+Label)
} else { } else {
bc.db.Model(&user).Update("state", "stringset:" + Label) bc.db.Model(&user).Update("state", "stringset:"+Label)
} }
bc.bot.Send(tgbotapi.NewMessage(user.ID, "Send me asset (text or picture (NOT as file))")) bc.bot.Send(tgbotapi.NewMessage(user.ID, "Send me asset (text or picture (NOT as file))"))
} }
} }
if user.IsAdmin() { if user.IsAdmin() {
if update.CallbackQuery.Data == "panel" { if update.CallbackQuery.Data == "panel" {
if !user.IsEffectiveAdmin() { if !user.IsEffectiveAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask | 0b10) bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b10)
msg := tgbotapi.NewMessage(user.ID, "You was in usermode, turned back to admin mode...") msg := tgbotapi.NewMessage(user.ID, "You was in usermode, turned back to admin mode...")
bc.bot.Send(msg) bc.bot.Send(msg)
} }
kbd := tgbotapi.NewInlineKeyboardMarkup( kbd := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Стартовая картинка", "update:preview_image")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Стартовая картинка", "update:preview_image")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Приветственный текст", "update:start")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Приветственный текст", "update:start")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Кнопка для заявки", "update:leave_ticket_button")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Кнопка для заявки", "update:leave_ticket_button")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID чата", "update:supportchatid")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID чата", "update:supportchatid")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID канала", "update:channelid")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("ID канала", "update:channelid")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Уведомление об отправке тикета", "update:sended_notify")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Уведомление об отправке тикета", "update:sended_notify")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба оставить тикет", "update:leaveticket_message")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба оставить тикет", "update:leaveticket_message")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба подписаться на канал", "update:subscribe_message")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Просьба подписаться на канал", "update:subscribe_message")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Ссылка на канал", "update:channel_link")), tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Ссылка на канал", "update:channel_link")),
) )
msg := tgbotapi.NewMessage(user.ID, "Выберите пункт для изменения") msg := tgbotapi.NewMessage(user.ID, "Выберите пункт для изменения")
msg.ReplyMarkup = kbd msg.ReplyMarkup = kbd
bc.bot.Send(msg) bc.bot.Send(msg)
} }
} }
canswer := tgbotapi.NewCallback(update.CallbackQuery.ID, "") canswer := tgbotapi.NewCallback(update.CallbackQuery.ID, "")
bc.bot.Send(canswer) bc.bot.Send(canswer)
} else if update.ChannelPost != nil { // TODO } else if update.ChannelPost != nil { // TODO
post := update.ChannelPost post := update.ChannelPost
if post.Text == "setchannelid" { if post.Text == "setchannelid" {
bc.SetBotContent("channelid", strconv.FormatInt(post.SenderChat.ID, 10)) bc.SetBotContent("channelid", strconv.FormatInt(post.SenderChat.ID, 10))
var admins []User var admins []User
bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins) bc.db.Where("role_bitmask & 1 = ?", 1).Find(&admins)
for _, admin := range admins { for _, admin := range admins {
bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is set to " + strconv.FormatInt(post.SenderChat.ID, 10))) bc.bot.Send(tgbotapi.NewMessage(admin.ID, "ChannelID is set to "+strconv.FormatInt(post.SenderChat.ID, 10)))
delcmd := tgbotapi.NewDeleteMessage(post.SenderChat.ID, post.MessageID) delcmd := tgbotapi.NewDeleteMessage(post.SenderChat.ID, post.MessageID)
bc.bot.Send(delcmd) bc.bot.Send(delcmd)
} }
} }
} }
} }
func DownloadFile(filepath string, url string) error { func DownloadFile(filepath string, url string) error {