Abel'Blog

我干了什么?究竟拿了时间换了什么?

0%

Telegram APIs 用法

简介

阅读Telegram APIs接入相关。

The_Botfather

Telegram APIs

我们为开发人员提供两种类型的 API。机器人 API 允许您轻松创建使用电报消息作为接口的程序。Telegram API和TDLib允许您构建自己的自定义Telegram客户端。欢迎您免费使用这两个 API。

您还可以将电报小部件添加到您的网站。

欢迎设计师为电报创建动画贴纸或自定义主题。

机器人接口

此 API 允许您将机器人连接到我们的系统。电报机器人是不需要额外电话号码即可设置的特殊帐户。这些帐户充当在服务器上某处运行的代码的接口。

要使用它,您无需了解我们的MTProto加密协议的工作原理 - 我们的中间服务器将为您处理与Telegram API的所有加密和通信。您可以通过一个简单的HTTPS接口与此服务器进行通信,该接口提供了Telegram API的简化版本。

回声机器人

参考代码

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
// 连接上你的bot
bot, err = tgbotapi.NewBotAPI("<YOUR_BOT_TOKEN_HERE>")
// 获取机器人的消息队列
updates := bot.GetUpdatesChan(u)

// 读取并且处理
case update := <-updates:
handleUpdate(update)

// 一共两类数据需要处理; messages 或者 button 点击事件
func handleUpdate(update tgbotapi.Update) {
switch {
// Handle messages
case update.Message != nil:
handleMessage(update.Message)
break

// Handle button clicks
case update.CallbackQuery != nil:
handleButton(update.CallbackQuery)
break
}
}

// 聊天将会直接将内容返回回去
func handleMessage(message *tgbotapi.Message) {
user := message.From
text := message.Text

if user == nil {
return
}

// Print to console
log.Printf("%s wrote %s", user.FirstName, text)

var err error
if strings.HasPrefix(text, "/") {
err = handleCommand(message.Chat.ID, text)
} else if screaming && len(text) > 0 {
msg := tgbotapi.NewMessage(message.Chat.ID, strings.ToUpper(text))
// To preserve markdown, we attach entities (bold, italic..)
msg.Entities = message.Entities
_, err = bot.Send(msg)
} else {
// This is equivalent to forwarding, without the sender's name
copyMsg := tgbotapi.NewCopyMessage(message.Chat.ID, message.Chat.ID, message.MessageID)
_, err = bot.CopyMessage(copyMsg)
}

if err != nil {
log.Printf("An error occured: %s", err.Error())
}
}

// 按钮响应函数将会拼接出html返回回去
func sendMenu(chatId int64) error {
msg := tgbotapi.NewMessage(chatId, firstMenu)
// 里面能支持 html/markdown/makrdown2
msg.ParseMode = tgbotapi.ModeHTML
msg.ReplyMarkup = firstMenuMarkup
_, err := bot.Send(msg)

return err
}

func sendKeyboard(chatId int64) error {
var itemsSel = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("button1"),
tgbotapi.NewKeyboardButton("button2")),
)
msg := tgbotapi.NewMessage(chatId, "Main Menu")
msg.ReplyMarkup = &tgbotapi.ReplyKeyboardMarkup{
Keyboard: itemsSel,
ResizeKeyboard: true,
OneTimeKeyboard: false,
Selective: false,
}
msg.ReplyMarkup = mainMenu
_, err := bot.Send(msg)

return err
}

// 定义一个 button
// Button texts
nextButton = "Next"
firstMenuMarkup = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(nextButton, nextButton),
),
)

// 判断是否按了这个按钮
func handleButton(query *tgbotapi.CallbackQuery) {
var text string

markup := tgbotapi.NewInlineKeyboardMarkup()
message := query.Message

if query.Data == nextButton {
text = secondMenu
markup = secondMenuMarkup
}
}

如何定制返回信息

1
2
// 支持返回三种格式的text,使用html比较简单;

如何获取机器人的 group 信息

https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
In order to get the group chat id, do as follows:
Add the Telegram BOT to the group.
Get the list of updates for your BOT:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Ex:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Look for the "chat" object:
{"update_id":8393,"message":{"message_id":3,"from":{"id":7474,"first_name":"AAA"},"chat":{"id":<group_ID>,"title":""},"date":25497,"new_chat_participant":{"id":71,"first_name":"NAME","username":"YOUR_BOT_NAME"}}}
This is a sample of the response when you add your BOT into a group.
Use the "id" of the "chat" object to send your messages.
# 这个问题是常见的,如果拿不到信息一般都是存在问题了
(If you created the new group with the bot and you only get {"ok":true,"result":[]}, remove and add the bot again to the group)
Private chart only works in image argoprojlabs/argocd-notifications:v1.1.0 or above.

Share
Improve this answer
Follow
edited Sep 9, 2022 at 16:31
Iulian Onofrei's user avatar
Iulian Onofrei
9,2281010 gold badges6767 silver badges114114 bronze badges

TDLib - 创建你自己的 Telgram(暂时不阅读)

检测加入群,退出群

1
2
3
4
5
6
7
8
9
10
11
12
case update.MyChatMember != nil:
if update.MyChatMember.NewChatMember.Status == "left" {
if update.MyChatMember.NewChatMember.User.ID == 机器人编号 &&
update.MyChatMember.Chat.Type == "group" {
update.MyChatMember.Chat.ID
}
} else if update.MyChatMember.NewChatMember.Status == "member" {
if update.MyChatMember.NewChatMember.User.ID == 机器人编号 &&
update.MyChatMember.Chat.Type == "group" {
update.MyChatMember.Chat.Title, update.MyChatMember.Chat.ID
}
}

好用的telegram框架

tucnak-telebot

参考