正如我在上一篇文章中提到的,我是 Python 新手,我为了工作机会而将学习转向 Python,并决定做一个练习,开发一个 Telegram 机器人。所以我编写了这些代码:
这是位于 api 文件夹中的 mainShineApi.py 文件
from cryptography.fernet import Fernet
import telebot
import os
def load_key():
current_dir = os.path.dirname(__file__)
key_path = os.path.join(current_dir, 'secret.key')
# Carrega a chave do arquivo
return open(key_path, 'rb').read()
def decrypt_api_key():
current_dir = os.path.dirname(__file__)
key = load_key() # Carrega a chave de criptografia
fernet = Fernet(key) # Cria um objeto Fernet com a chave
# Carrega a API key criptografada do arquivo
encrypted_api_key_path = os.path.join(current_dir, 'encrypted_api_key.bin')
with open(encrypted_api_key_path, 'rb') as encrypted_file:
encrypted_api_key = encrypted_file.read()
# Descriptografa a API key
decrypted_api_key = fernet.decrypt(encrypted_api_key)
return decrypted_api_key.decode()
def botApi():
SHINEKUNAPI = decrypt_api_key()
bot = telebot.TeleBot(SHINEKUNAPI)
return botApi
bot = botApi()
这是位于主文件夹中的 handlers.py 文件:
from api.shineMainApi import bot
from api.mainApi import apiSerasa
import telebot
@bot.message_handler(commands=['serasa'])
def serasaMessage(message):
try:
# Extrai os parâmetros da mensagem
_, tel, cpf, nome, email = message.text.split()
# Chama a função apiSerasa com os parâmetros extraídos
resultados = apiSerasa(tel, cpf, nome, email)
# Formata a resposta
resposta = (f"Resultados da consulta:\n\n"
f"Telefone: {resultados['telefone']}\n"
f"CPF: {resultados['cpf']}\n"
f"Nome: {resultados['nome']}\n"
f"Email: {resultados['email']}")
bot.reply_to(message, resposta)
except ValueError:
bot.reply_to(message, "Por favor, forneça os parâmetros no formato correto:\n/serasa telefone cpf nome email")
except Exception as e:
bot.reply_to(message, f"Ocorreu um erro: {str(e)}")
@bot.message_handler(commands=['consultas'])
def consultasMessage(message):
text = """
Nós temos varios tipos de consulta, escolha a sua opção:
/serasa consultar pelo serasa
"""
bot.reply_to(message, text)
@bot.message_handler(commands=['github'])
def send_github_link(message):
github_url = "https://github.com/Shine-Kun-Bot/Shine-Kun-Bot"
bot.reply_to(message, f"Acesse o repositório do projeto no GitHub: {github_url}")
@bot.message_handler(commands=['start'])
def respostaPadrao(mensagem):
texto = """
Olá, sou o shine kun bot! Como vai? Escolha uma das opções do que você precisa
/consultas abre um menu das opções de consultas que você tem
/github abre o github do nosso projeto
"""
bot.reply_to(mensagem, texto)
这是 main.py 文件:
import telebot
import handlers
from api.mainApi import apiSerasa
from api.shineMainApi import bot
if __name__ == "__main__":
bot.polling()
它返回这个错误,但在IDE中它没有显示任何明显的错误
@bot.message_handler(commands=['serasa']) ^^^^^^^^^^^^^^^^^^^ AttributeError: 'function' object has no attribute 'message_handler'
我已经研究并询问了 gpt 聊天,但我找不到解决方案,我想为必须在同一天发表两篇文章而道歉,但如果我在这里问,那是因为我真的不知道'不知道怎么解决。
当您调用 botApi 时,您将返回函数 botAPi。你需要做退货机器人
所以你需要更改你的代码:
def botApi():
SHINEKUNAPI = decrypt_api_key()
bot = telebot.TeleBot(SHINEKUNAPI)
return botApi
进入这个
def botApi():
SHINEKUNAPI = decrypt_api_key()
bot = telebot.TeleBot(SHINEKUNAPI)
return bot