gotgbot

package
v1.2.65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Golang 控制tg相关api封装

package main

import (
	golog "github.com/gif-gif/go.io/go-log"
	gotgbot "github.com/gif-gif/go.io/go-tgbot"
	"github.com/gogf/gf/util/gconv"
	tele "gopkg.in/telebot.v3"
	"time"
)

// Recipient returns chat ID (see Recipient interface).

func main() {
	var chatId int64 = 5562314141
	pref := &gotgbot.TelegramBot{
		Product:    "test",
		Token:      "Token",
		WebAppUrl:  "https://www.google.com",
		StartReply: "hello",
	}

	gobot, err := gotgbot.CreateOfflineBot(pref) // TODO离线模式测试
	if err != nil {
		golog.Error(err.Error())
		return
	}
	var (
		// Universal markup builders.
		menu = &tele.ReplyMarkup{
			InlineKeyboard: [][]tele.InlineButton{{
				{
					Text:   "This is a web app",
					WebApp: &tele.WebApp{URL: "https://google.com"},
				},
			}},
		}
	)

	gobot.Handle("/start", func(c tele.Context) error {
		return c.Send(pref.StartReply, menu)
	})

	var trace []string

	handler := func(name string) tele.HandlerFunc {
		return func(c tele.Context) error {
			trace = append(trace, name)
			return nil
		}
	}

	middleware := func(c tele.Context, next tele.HandlerFunc, params ...any) error {
		trace = append(trace, gconv.String(params[0])+":in")
		err := next(c)
		trace = append(trace, gconv.String(params[0])+":out")
		return err
	}

	createMiddleware1 := gobot.CreateMiddleware(middleware, "handler1a")
	createMiddleware2 := gobot.CreateMiddleware(middleware, "handler2a")
	createGroup1Middleware1 := gobot.CreateMiddleware(middleware, "group1")
	createGroup2Middleware2 := gobot.CreateMiddleware(middleware, "group2")
	createGroup2Middleware3 := gobot.CreateMiddleware(middleware, "handler1b")

	b := gobot.GetBot()
	gobot.UseMiddleware(middleware, "test")
	gobot.UseMiddleware(middleware, "global1")
	gobot.UseMiddleware(middleware, "global2")

	b.Handle("/a", handler("/a"), createMiddleware1, createMiddleware2)

	group := b.Group()
	group.Use(createGroup1Middleware1, createGroup2Middleware2)
	group.Handle("/b", handler("/b"), createGroup2Middleware3)

	b.ProcessUpdate(tele.Update{
		Message: &tele.Message{Text: "/a"},
	})

	golog.WithTag("gotgbot").Info(trace)
	trace = trace[:0]
	b.ProcessUpdate(tele.Update{
		Message: &tele.Message{Text: "/b"},
	})
	golog.WithTag("gotgbot").Info(trace)

	gobot.SendMsgText(chatId, "text")
	gobot.SendFromUrlPhotos(gotgbot.ChatID(chatId), []string{"https://developer.android.google.cn/static/studio/images/run/adb_wifi-quick_settings.png?hl=zh-cn"})

	go gobot.StartBot()

	time.Sleep(1000 * time.Second)
}

bot 仓库

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatID

type ChatID int64

func (ChatID) Recipient

func (i ChatID) Recipient() string

Recipient returns chat ID (see Recipient interface).

type GoTgBot

type GoTgBot struct {
	// contains filtered or unexported fields
}

func Create

func Create(config *TelegramBot) (*GoTgBot, error)

同一个产品只会存在一个

func CreateOfflineBot

func CreateOfflineBot(config *TelegramBot) (*GoTgBot, error)

离线测试, 模拟用户发命令

b.ProcessUpdate(tele.Update{
	Message: &tele.Message{Text: "/a"},
})

func (*GoTgBot) BanSenderChat

func (g *GoTgBot) BanSenderChat(chat *telebot.Chat, sender telebot.Recipient) error

func (*GoTgBot) CheckSign added in v1.2.17

func (g *GoTgBot) CheckSign(ts int64, sign string) bool

func (*GoTgBot) CreateMiddleware

func (g *GoTgBot) CreateMiddleware(middleware func(c telebot.Context, next telebot.HandlerFunc, parameters ...any) error, parameters ...any) telebot.MiddlewareFunc

func (*GoTgBot) CreateMyAccountCommand added in v1.2.14

func (g *GoTgBot) CreateMyAccountCommand(commandText string)

func (*GoTgBot) CreateWebAppStartCommand added in v1.2.14

func (g *GoTgBot) CreateWebAppStartCommand(menuButtonText string)

/start

func (*GoTgBot) GetBot

func (g *GoTgBot) GetBot() *telebot.Bot

func (*GoTgBot) GetConfig

func (g *GoTgBot) GetConfig() *TelegramBot

func (*GoTgBot) Handle

func (g *GoTgBot) Handle(endpoint interface{}, h telebot.HandlerFunc, m ...telebot.MiddlewareFunc)

func (*GoTgBot) ReplyMarkup

func (g *GoTgBot) ReplyMarkup() *telebot.ReplyMarkup

func (*GoTgBot) Send

func (g *GoTgBot) Send(to telebot.Recipient, what interface{}, opts ...interface{}) (*telebot.Message, error)

func (*GoTgBot) SendAlbum

func (g *GoTgBot) SendAlbum(to telebot.Recipient, a telebot.Album, opts ...interface{}) ([]telebot.Message, error)

func (*GoTgBot) SendFromDiskAudios

func (g *GoTgBot) SendFromDiskAudios(to telebot.Recipient, fromDiskFiles []string) ([]telebot.Message, error)

func (*GoTgBot) SendFromDiskPhotos

func (g *GoTgBot) SendFromDiskPhotos(to telebot.Recipient, fromDiskFiles []string) ([]telebot.Message, error)

func (*GoTgBot) SendFromDiskVideos

func (g *GoTgBot) SendFromDiskVideos(to telebot.Recipient, fromDiskFiles []string) ([]telebot.Message, error)

func (*GoTgBot) SendFromUrlAudios

func (g *GoTgBot) SendFromUrlAudios(to telebot.Recipient, urls []string) ([]telebot.Message, error)

func (*GoTgBot) SendFromUrlPhotos

func (g *GoTgBot) SendFromUrlPhotos(to telebot.Recipient, urls []string) ([]telebot.Message, error)

func (*GoTgBot) SendFromUrlVideos

func (g *GoTgBot) SendFromUrlVideos(to telebot.Recipient, urls []string) ([]telebot.Message, error)

func (*GoTgBot) SendMsgText

func (g *GoTgBot) SendMsgText(chatId int64, msg string, opts ...interface{}) (*telebot.Message, error)

func (*GoTgBot) StartBot

func (g *GoTgBot) StartBot()

func (*GoTgBot) StopBot

func (g *GoTgBot) StopBot()

func (*GoTgBot) Use

func (g *GoTgBot) Use(middleware ...telebot.MiddlewareFunc)

func (*GoTgBot) UseMiddleware

func (g *GoTgBot) UseMiddleware(middleware func(c telebot.Context, next telebot.HandlerFunc, parameters ...any) error, parameters ...any)

type GoTgBots

type GoTgBots struct {
	// contains filtered or unexported fields
}

func New

func New() *GoTgBots

func (*GoTgBots) CreateBot

func (g *GoTgBots) CreateBot(config *TelegramBot) (*GoTgBot, error)

同一个产品只会存在一个,后一个添加的覆盖前面添加

func (*GoTgBots) DestroyBot

func (g *GoTgBots) DestroyBot(product string)

func (*GoTgBots) GetBot

func (g *GoTgBots) GetBot(product string) *GoTgBot

func (*GoTgBots) StartBot

func (g *GoTgBots) StartBot(product string)

func (*GoTgBots) StopBot

func (g *GoTgBots) StopBot(product string)

type TelegramBot

type TelegramBot struct {
	Product     string            `yaml:"Product" json:"product"`
	ApiUrl      string            `yaml:"ApiUrl" json:"apiUrl,optional"`
	ParseMode   telebot.ParseMode `yaml:"ParseMode" json:"parseMode,optional"`
	Token       string            `yaml:"Token" json:"token"`
	Timeout     int64             `yaml:"Timeout" json:"timeout,optional"` //s tg bot api timeout
	Offline     bool              `yaml:"Offline" json:"offline,optional"`
	Synchronous bool              `yaml:"Synchronous" json:"synchronous,optional"`

	// Product configuration (可选)
	Open         bool   `yaml:"Open" json:"open,optional"` //是否开启
	WebAppUrl    string `yaml:"WebAppUrl" json:"webAppUrl,optional"`
	StartReply   string `yaml:"StartReply" json:"startReply,optional"`
	SignSecret   string `yaml:"SignSecret" json:"signSecret,optional"`     //请求签名密钥
	SignTimeout  int64  `yaml:"SignTimeout" json:"signTimeout,optional"`   //请求签名过期时间-秒
	AccessSecret string `yaml:"AccessSecret" json:"accessSecret,optional"` //jwt signature secret
	AccessExpire int64  `yaml:"AccessExpire" json:"accessExpire,optional"` // jwt signature accessExpire
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL