May 15, 2024

使用Telegram Bot 机器人打造专属ChatGPT二道贩子(一)

TelegramBot教程 打造专属于你的GPT-4o助理。实现在移动端顺畅使用GPT-4o.

使用Telegram Bot 机器人打造专属ChatGPT二道贩子(一)

近日ChatGPT更新了GPT-4o模型,体验了一下感觉很不错,尤其是图片和文档识别分析的能力都有明显提升,主要是API价格又降低了。

但GPT-4o模型对所有人开放有点背刺我这种从一开始就氪金支持PLUS的老冤种用户了。

为什么要打造这个机器人呢?

ChatGPT的桌面Web版我一直觉得是体验最好的,但移动端web和客户端就有点难受。尤其是客户端的节点问题,经常导致登录困难,同一个IP在Web上没有任何访问问题,但在iOS上有时候怎么样也登不上。

于是乎,我就有了打造第三方客户端的念头。需求很简单,专属,易用,轻量,相应速度快就行。结合使用TELEGRAM MTPROXY可以在不使用其他任何代理App的情况下最高效的使用GPT-4o。因此,使用Telegrambot就成了最佳方案。


ChatGPT GPT-4o纯文本模型二道贩子部署教程

首先:创建Telegram机器人并获取API密钥

  1. 创建Telegram机器人
    • 在Telegram中搜索并启动与@BotFather的对话。
    • 使用 /newbot 命令创建一个新的机器人,按照提示完成设置并获取API密钥。
    • 后期可以给机器人改名上传头像,以及设置/getid /start /newchat 等菜单键。
  2. 获取OpenAI API密钥
    • 注册并登录OpenAI平台,然后在API页面获取API密钥。

登录VPS

使用SSH客户端(如PuTTY或终端)连接到你的VPS。你需要VPS的IP地址、用户名和密码。

ssh root@your_vps_ip

更新系统软件包

sudo apt update
sudo apt upgrade -y

安装Python和pip

sudo apt install python3 python3-pip -y

安装所需的Python库

pip3 install python-telegram-bot openai

编写并运行机器人代码

创建Python脚本

  • 在VPS上创建一个新的Python脚本文件,例如 telegram_bot.py
nano telegram_bot.py

编写机器人代码

  • 将以下代码复制到 telegram_bot.py 文件中,替换你的API并保存。
import os
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import openai

# 设置Telegram Bot的API密钥
TELEGRAM_API_KEY = '你的telegrambot-api密钥'
updater = Updater(token=TELEGRAM_API_KEY, use_context=True)

# 设置OpenAI API密钥
OPENAI_API_KEY = '你的openai-api密钥'
openai.api_key = OPENAI_API_KEY

# 定义允许的用户ID列表(白名单)
ALLOWED_USERS = {123456789, 987654321}  # 替换为你允许的用户ID

# 存储用户对话上下文
user_contexts = {}

# 处理/start命令
def start(update: Update, context: CallbackContext) -> None:
    user_id = update.message.from_user.id
    if user_id in ALLOWED_USERS:
        update.message.reply_text('你好!我是ChatGPT倒爷,本倒爷倒GPT-4o模型。')
    else:
        update.message.reply_text('抱歉,你没有权限使用这个机器人。')

# 处理/getid命令
def getid(update: Update, context: CallbackContext) -> None:
    user_id = update.message.from_user.id
    update.message.reply_text(f'你的用户ID是:{user_id}')

# 处理/newchat命令
def newchat(update: Update, context: CallbackContext) -> None:
    user_id = update.message.from_user.id
    if user_id in ALLOWED_USERS:
        user_contexts[user_id] = [{"role": "system", "content": "You are a helpful assistant."}]
        update.message.reply_text('新的对话已开启。')
    else:
        update.message.reply_text('抱歉,你没有权限使用这个机器人。')

# 处理文本消息
def echo(update: Update, context: CallbackContext) -> None:
    user_id = update.message.from_user.id
    if user_id in ALLOWED_USERS:
        user_input = update.message.text
        if user_id not in user_contexts:
            user_contexts[user_id] = [{"role": "system", "content": "You are a helpful assistant."}]
        
        user_contexts[user_id].append({"role": "user", "content": user_input})
        
        # 调用OpenAI的API生成回复,使用GPT-4o
        response = openai.ChatCompletion.create(
            model="gpt-4o",
            messages=user_contexts[user_id]
        )
        generated_text = response['choices'][0]['message']['content'].strip()
        
        user_contexts[user_id].append({"role": "assistant", "content": generated_text})
        
        update.message.reply_text(generated_text)
    else:
        update.message.reply_text('抱歉,你没有权限使用这个机器人。')

# 设置命令处理程序
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("getid", getid))
dispatcher.add_handler(CommandHandler("newchat", newchat))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

# 启动机器人
updater.start_polling()
updater.idle()

运行py测试运行情况

python3 telegram_bot.py

在Telegram Bot上,发送 /getid 获得你的 user_id ,将你的 user_id 填入 telegram_bot.py 的第15行。重新运行py。

使用/start 开始使用机器人,

使用/getid 获取用户ID,

使用/newchat 开始新的对话。

Debug无问题,结束进程,设置常驻后台,并记录log。

nohup python3 telegram_bot.py > bot.log 2>&1 &

就此,使用Telegram Bot 机器人打造的专属ChatGPT(GPT-4o 纯文字)二道贩子就完成了。