Telebot is a Telegram bot framework in Go.

先登录telegram创建机器人,创建机器人获取到 ,可以通过API查询。

1
curl -s 'https://api.telegram.org/bot<TOKEN>/getMe'

响应结果

1
{"ok":true,"result":{"id":6906901111,"is_bot":true,"first_name":"test","username":"test","can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false}}

实现电报机器人示例:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
	"fmt"
	"time"

	tele "gopkg.in/telebot.v3"
	"gopkg.in/telebot.v3/middleware"
)

func main() {
	b, err := tele.NewBot(tele.Settings{
		Token:  "<TOKEN>",
		Poller: &tele.LongPoller{Timeout: 10 * time.Second},
	})
	if err != nil {
		return
	}

	// Global-scoped middleware:
	b.Use(middleware.Logger())
	b.Use(middleware.AutoRespond())

	b.Handle("/start", func(c tele.Context) error {
		return c.Send("Hello world!")
	})

	// Command: /hello <PAYLOAD>
	b.Handle("/hello", func(c tele.Context) error {
		fmt.Println(c.Message().Payload) // <PAYLOAD>
		return c.Send("Hello world!" + c.Message().Payload)
	})

	b.Handle("/member", func(c tele.Context) error {
		fmt.Println(c.Message().Payload) // <PAYLOAD>

		chat := c.Chat()
		memberList, err := b.AdminsOf(chat)
		fmt.Printf("memberList: %d, err: %v\n", len(memberList), err)
		for _, v := range memberList {
			fmt.Printf("user: %s\n", v.User.Username)
		}

		return c.Send("Hello world!" + c.Message().Payload)
	})

	b.Handle(tele.OnText, func(c tele.Context) error {
		// All the text messages that weren't
		// captured by existing handlers.

		var (
			user = c.Sender()
			text = c.Text()
		)
		fmt.Println("text ", text)

		// Use full-fledged bot's functions
		// only if you need a result:
		msg, err := b.Send(user, text)
		if err != nil {
			return err
		}
		_ = msg
		// fmt.Printf("msg: %#v, user: %#v, text: %s\n", msg, user, text)

		// Instead, prefer a context short-hand:
		return c.Send(text)
	})

	// 在群组中将机器人添加为管理员。
	b.Handle(tele.OnChannelPost, func(c tele.Context) error {
		// Channel posts only.
		msg := c.Message()
		fmt.Printf("channel msg: %#v\n", msg)

		chat := c.Chat()
		memberList, err := b.AdminsOf(chat)
		fmt.Printf("memberList: %d, err: %s\n", len(memberList), err)
		for _, v := range memberList {
			fmt.Printf("user: %s\n", v.User.Username)
		}
		return nil
	})

	var (
		// Universal markup builders.
		menu     = &tele.ReplyMarkup{ResizeKeyboard: true}
		selector = &tele.ReplyMarkup{}

		// Reply buttons.
		btnHelp     = menu.Text("ℹ Help")
		btnSettings = menu.Text("⚙ Settings")

		// Inline buttons.
		//
		// Pressing it will cause the client to
		// send the bot a callback.
		//
		// Make sure Unique stays unique as per button kind
		// since it's required for callback routing to work.
		//
		btnPrev = selector.Data("⬅", "prev")
		btnNext = selector.Data("➡", "next")
	)

	menu.Reply(
		menu.Row(btnHelp),
		menu.Row(btnSettings),
	)
	selector.Inline(
		selector.Row(btnPrev, btnNext),
	)

	b.Handle("/menu", func(c tele.Context) error {
		return c.Send("Hello!", menu)
	})

	r := b.NewMarkup()

	// Reply buttons:
	r.Reply(r.Row(
		r.Text("Hello!"),
		r.Contact("Send phone number"),
		r.Location("Send location"),
		r.Poll("poll", tele.PollQuiz), // 设置问题
	))

	// Inline buttons:
	// r.Inline(r.Row(
	// 	r.Data("Show help", "help"), // data is optional
	// 	r.Data("Delete item", "delete"),
	// 	r.URL("Visit", "https://google.com"),
	// 	r.Query("Search", "query"),
	// 	r.QueryChat("Share", "query"),
	// 	r.Login("Login", &tele.Login{URL: "https://google.com"}),
	// ))

	b.Handle("/markup", func(c tele.Context) error {
		return c.Send("Hello!", r)
	})

	b.Start()
}
 

运行后,即可登录电报给机器人发送命令。

参考