Telegram Bot(一)Python库pyTelegramBotAPI的使用

通过Telegram提供的Bot API可以实现Bot和用户的交互,Python库pyTelegramBotAPI对Bot API进行了封装,使得我们可以通过调用Python函数的形式对API进行调用

基于Python的Telegram Bot,使用pyTelegramBotAPI库。

准备

创建Bot

点击START私聊@BotFather,输入/newbot开始创建Bot。

新建Bot

等到BotFather回复后输入想要创建的Bot名称,BotFather再次回复后输入Bot的username,注意username必须以_bot结尾。

创建完Bot后就可以获取到Bot的token,Telegram Bot API的调用必须提供token。

安装pyTelegramBotAPI

使用pip安装pyTelegramBotAPI。

1
pip install pyTelegramBotAPI

Bot的运行原理

Bot和一般的用户一样,都有名称和id,可以通过@username查找到Bot。Bot的运行是通过和所有者服务器上的脚本联合实现的,用户通过Telegram向Bot发送信息,Bot的脚本根据Bot的token,就可以在服务器上通过Telegram提供的Bot API获取到用户发送给Bot的信息,从而实现根据用户发送的信息返回给用户相应的回复。

pyTelegramBotAPI是一个Python库,对Telegram Bot API进行了封装。使得开发者不用对API直接调用,使用pyTelegramBotAPI提供的函数和类就可以实现Telegram Bot API的各种功能。

pyTelegramBotAPI的基础使用

新建bot对象

pyTelegramBotAPI中调用API的methods全部定义在TeleBot类中,通过传入Bot token新建bot对象。

1
2
3
import telebot

bot = telebot.TeleBot("TOKEN", parse_mode=None) # You can set parse_mode by default. HTML or MARKDOWN

TeleBot类中的函数和Telegram Bot API提供的method同名,但是更改了命名规则,例如API中的getMe在TeleBot类对应于get_me,API中的sendMessage被命名为send_message

Bot提供给用户的回复是通过handler实现的,对于用户发送给Bot的消息,使用message handler进行处理。

1
2
3
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
	bot.reply_to(message, "Howdy, how are you doing?")

上述代码中表示对于用户发送给Bot的消息,定义一个message_handler,这个message_handler在用户发送了指令/start或/help时执行send_welcome函数,产生对用户的回复”Howdy, how are you doing?“。

message对象

用户对Bot发送消息后,通过Telegram Bot API获得到的是一个名为message的JSON对象,包含了消息内容,用户id等相关信息,详情可参考Telegram官方文档。

在上述给出的message_handler示例代码中,send_welcome函数传入参数message对象,reply_to通过message中包含的属性chat.id等获取用户id,从而向用户发送回复信息。

需要注意的是,由于from是Python中的保留字,在pyTelegramBotAPI中,message的属性名from被替换为from_user

message handler

message handler用来对用户发送的message类消息做出反应(用户发送的消息一般都为message,除此之外还有投票poll、频道推文channel post等其他类型),用于处理其他类消息的handler还有poll handler、channel post handler等。

1
2
3
@bot.message_handler(filters)
def function_name(message):
	bot.reply_to(message, "This is a message handler")

在message handler的一般形式中,filters用来对message进行筛选,使该message_handler只对特定类型的message进行响应。filters的一般格式为name=argument,name表示筛选类型,argument代表筛选范围。例如commands=['start', 'help']表示只对命令/start和/help做出反应。

官方给出的filters支持的类型如下:

name argument Condition
content_types list of strings (default [’text’]) True if message.content_type is in the list of strings.
regexp a regular expression as a string True if re.search(regexp_arg) returns True and message.content_type == ’text’ (See https://docs.python.org/2/library/re.html)
commands list of strings True if message.content_type == ’text’ and message.text starts with a command that is in the list of strings.
chat_types list of chat types True if message.chat.type in your filter
func a function (lambda or function reference) True if the lambda or function reference returns True
 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
import telebot
bot = telebot.TeleBot("TOKEN")

# 只处理命令 '/start' 和 '/help'
@bot.message_handler(commands=['start', 'help'])
def handle_start_help(message):
	pass

# 只处理用户发送内容为“文件”和“音频”的message
@bot.message_handler(content_types=['document', 'audio'])
def handle_docs_audio(message):
	pass

# Handles all text messages that match the regular expression
@bot.message_handler(regexp="SOME_REGEXP")
def handle_message(message):
	pass

# Handles all messages for which the lambda returns True
@bot.message_handler(func=lambda message: message.document.mime_type == 'text/plain', content_types=['document'])
def handle_text_doc(message):
	pass

# Which could also be defined as:
def test_message(message):
	return message.document.mime_type == 'text/plain'

@bot.message_handler(func=test_message, content_types=['document'])
def handle_text_doc(message):
	pass

# Handlers can be stacked to create a function which will be called if either message_handler is eligible
# This handler will be called if the message starts with '/hello' OR is some emoji
@bot.message_handler(commands=['hello'])
@bot.message_handler(func=lambda msg: msg.text.encode("utf-8") == SOME_FANCY_EMOJI)
def send_something(message):
    pass
Built with Hugo
主题 StackJimmy 设计