Post

python-telegram-bot接收指令一

参考:

一、安装python-telegram-bot

1
2
3
4
# python 2.x环境的
pip install python-telegram-bot --upgrade
# python 3.x环境的
pip3 install python-telegram-bot --upgrade

二、简单的接收指令及解析消息的例子testBot1.py

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
#!/usr/bin/python
# -*- coding: UTF-8 -*-

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler

# 处理 /start 命令
async def start(update: Update, context) -> None:
    print(update)
    # print("======message======")
    # print(update.message)
    print("=======from_user=====")
    print(update.message.from_user)
    print("======chat======")
    print(update.message.chat)
    print("======text======")
    print(update.message.text)
    # 回复消息
    await update.message.reply_text('你好!我是你的 Telegram 机器人!=>' + update.message.text)

if __name__ == '__main__':
    application = ApplicationBuilder().token('your token').build()

    # 添加 /start 命令处理器
    application.add_handler(CommandHandler('start', start))

    # 运行机器人
    application.run_polling()

# python3 /xxx/xxx/testBot1.py
# ctrl + c 停止运行

image

image


三、展示键盘选择的例子testBot2.py

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
#!/usr/bin/python
# -*- coding: UTF-8 -*-

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ApplicationBuilder, CommandHandler, CallbackQueryHandler

# 处理 /start 命令
async def start(update: Update, context) -> None:
    # print("=====start=update======")
    # print(update)
    # print("======message======")
    # print(update.message)
    print("=======from_user=====")
    print(update.message.from_user)
    print("======chat======")
    print(update.message.chat)
    print("======text======")
    print(update.message.text)
    keyboard = [[InlineKeyboardButton("选项 1", callback_data='1'),
                 InlineKeyboardButton("选项 2", callback_data='2')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    await update.message.reply_text('请选择一个选项:', reply_markup=reply_markup)

# 处理按钮回调
async def button(update: Update, context) -> None:
    # print("=====button=update======")
    # print(update)
    print("=======callback_query=====")
    print(update.callback_query)
    print("=======data=====")
    print(update.callback_query.data)
    print("=======message=====")
    print(update.callback_query.message)
    print("=======from_user=====")
    print(update.callback_query.from_user)
    query = update.callback_query
    await query.answer()
    # 编辑回复
    await query.edit_message_text(text=f"你选择了选项 {query.data}")

if __name__ == '__main__':
    application = ApplicationBuilder().token('your token').build()

     # 添加处理命令和按钮的处理器
    application.add_handler(CommandHandler('start', start))
    application.add_handler(CallbackQueryHandler(button))

    # 运行机器人
    application.run_polling()

# python3 /xxx/xxx/testBot2.py
# ctrl + c 停止运行

This post is licensed under CC BY 4.0 by the author.