创作背景
我为什么要做一个TG机器人?

首先是因为国内的QQ/wechat都没有给出官方的api文档允许用户自己去创造一个机器人,而telegram的可玩性更高,有更加开放的社区生态,不会动不动封号/doge

第二呢就是tg上有很多类似的机器人,并且做一个这种机器人去管理你的频道是有利可图的,频道广告能使你获得不错的收益!

那些做发片的channle懂得都懂,动辄几十K的订阅,当然telegram bot不止这种用途,他有比较完整的API文档供你参考,但是不好的一点就是,他只有英文文档和only_english的tg群

言归正传

附上我的tg bot使用截图和相关代码参考,后期我会进一步改进它,并出视频教程!

使用截图

使用python-telegram-bot做一个影视机器人_telegrambot

使用python-telegram-bot做一个影视机器人_API_02

代码
import logging
import random
import requests
from telegram import Update, Bot
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, CallbackContext

# 打印日志方法
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)

# 定义电影API接口
MOVIE_API_URL = "https://xiaoapi.cn/API/jx_txsp.php"
# 定义电影盘API接口
PAN_API_URL = "https://www.hhlqilongzhu.cn/api/ziyuan_nanfeng.php"


# 调用电影API接口获取电影数据
async def get_movie_data(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 获取用户输入的电影url
    movie_url = update.message.text
    # 发送请求到API接口
    response = requests.get(MOVIE_API_URL, params=movie_url)
    # 检查响应状态码
    if response.status_code == 200:
        movie_info = response.json()  # 假设API返回的是JSON格式数据
        # 检查是否有电影数据返回
        if movie_info and 'title' in movie_info:
            # 发送电影信息给用户
            await update.message.reply_text("解析成功啦!")
            await update.message.reply_text(
                "点击这里访问 [" + movie_info['title'] + "]" + "(" + movie_info['url'] + ")", parse_mode='Markdown')
        else:
            await update.message.reply_text("抱歉,没有找到相关的影片解析信息。")
    else:
        await update.message.reply_text("无法连接到电影API接口。")

# 调用电影网盘资源API接口获取电影数据
async def get_movieName_data(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 获取用户输入的电影名称
    movie_name = update.message.text
    # 发送请求到API接口
    response = requests.get(PAN_API_URL, params={'keysearch': movie_name})
    # 检查响应状态码
    if response.status_code == 200:
        movie_info = response.json()  # 假设API返回的是JSON格式数据
        # 检查是否有电影数据返回
        if 'data' in movie_info:
            await update.message.reply_text("获取资源成功" + "! 共找到" + str(movie_info['count']) + '条资源')
            # 发送电影信息给用户
            num = 0
            mastext = []
            for i in movie_info['data']:
                num += 1
                mastext.append(f"{num}:{i['title']}-({i['data_url']})  ")
            await update.message.reply_text('\n'.join(mastext), parse_mode=None)
        else:
            await update.message.reply_text("抱歉,没有找到相关的影片信息。")
    else:
        await update.message.reply_text("无法连接到PAN_API_URL接口。")


# CommandHandler 一些命令方法
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    '''响应start命令'''
    text = '欢迎使用可搜机器人,您可以发送VIP视频链接给我解析'
    await context.bot.send_message(chat_id=update.effective_chat.id, text=text)


async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
    '''响应help命令'''
    text = "=" * 5 + '开发者信息' + "=" * 5 + "\n" + "Developer:RenYuke"
    await context.bot.send_message(chat_id=update.effective_chat.id, text=text)


async def ohayo(update: Update, context: ContextTypes.DEFAULT_TYPE):
    '''随机消息'''
    texts = ['早上好呀', '我的小鱼你醒了,还记得清晨吗', '哦哈哟~']
    await context.bot.send_message(chat_id=update.effective_chat.id, text=random.choice(texts))

# 判断用户发送的消息是URL还是电影名称去选择对应的处理方法
async def choose_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 检查消息是否为文本消息
    if update.message.text:
        text = update.message.text
        # 检查文本是否以https开头
        if text.startswith('https://'):
            # 是URL,调用处理URL的函数
            await get_movie_data(update, context)
        else:
            # 不是URL,调用处理电影名称的函数
            await get_movieName_data(update, context)


def main():
    # 命令响应
    start_handler = CommandHandler('start', start)
    help_handler = CommandHandler('help', help)
    # 消息匹配
    filter_ohayo = filters.Regex('早安|早上好|哦哈哟|ohayo|早')
    ohayo_handler = MessageHandler(filter_ohayo, ohayo)

    # 添加消息处理器,用于处理用户发送的文本消息
    massage_handle = MessageHandler(filters.TEXT & ~filters.COMMAND, choose_handler)

    # 构建 bot 请在TOKEN中替换为你自己的机器人秘钥
    TOKEN = 'YOUR TOKEN'
    application = ApplicationBuilder().token(TOKEN).build()
    # 注册 handler
    application.add_handler(start_handler)
    application.add_handler(help_handler)
    application.add_handler(ohayo_handler)
    application.add_handler(massage_handle)

    # run!
    application.run_polling()


if __name__ == '__main__':
    main()
  • 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.
使用的API
电影wangpanAPI

URL: https://www.hhlqilongzhu.cn/api/ziyuan_nanfeng.php

请求参数

接口名称

是否必填

接口类型

接口说明

keysearch


string

关键词

使用python-telegram-bot做一个影视机器人_telegrambot_03

返回参数

参数名称

参数类型

参数说明

title

text

视频标题

data_url

text

视频资源链接

返回示例
{
    "data": [
        {
            "title": "[龙珠超][2015][全集][动画]",
            "data_url": "链接:xxxx"
        },
        {
            "title": "[龙珠超:超级英雄][2022][动画][日本]",
            "data_url": "链接:xxxx 提取码:xxxx"
        }
    ],
    "count": 2
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
电影URL解析API

URL: https://xiaoapi.cn/API/jx_txsp.php

请求参数

参数名称

参数类型

是否必填

备注内容

url

text

需要解析的播放链接

返回参数

参数名称

参数类型

参数说明

code

text

状态码

title

text

视频标题

url

text

视频资源链接

返回示例
{
    "code": 200,
    "title": "破·局",
    "url": "xxxxxxxx"
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.