我如何将这两个文件合并为一个用于电报机器人

问题描述 投票:0回答:0

作为 python 的初学者,我不知道如何合并这两个文件有人可以帮助我这对我也更有用,如果可能的话,我需要在这里添加 Json,但我没有知道怎么做。

例如,在 Json 中,用户的问题应该是“嗨”,答案应该是“你好吗”。

像这样在Json中如何创建它请帮助

第一个代码是telegram.py

from typing import Final

# pip install python-telegram-bot
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes

print('Starting up bot...')

TOKEN: Final = 'YOUR TOKEN'
BOT_USERNAME: Final = '@your_bot_user'


# Lets us use the /start command
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text('Hello there! I\'m a bot. What\'s up?')


# Lets us use the /help command
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text('Try typing anything and I will do my best to respond!')


# Lets us use the /custom command
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text('This is a custom command, you can add whatever text you want here.')


def handle_response(text: str) -> str:
    # Create your own response logic
    processed: str = text.lower()

    if 'hello' in processed:
        return 'Hey there!'

    if 'how are you' in processed:
        return 'I\'m good!'

    if 'i love python' in processed:
        return 'Remember to subscribe!'

    return 'I don\'t understand'


async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # Get basic info of the incoming message
    message_type: str = update.message.chat.type
    text: str = update.message.text

    # Print a log for debugging
    print(f'User ({update.message.chat.id}) in {message_type}: "{text}"')

    # React to group messages only if users mention the bot directly
    if message_type == 'group':
        # Replace with your bot username
        if BOT_USERNAME in text:
            new_text: str = text.replace(BOT_USERNAME, '').strip()
            response: str = handle_response(new_text)
        else:
            return  # We don't want the bot respond if it's not mentioned in the group
    else:
        response: str = handle_response(text)

    # Reply normal if the message is in private
    print('Bot:', response)
    await update.message.reply_text(response)


# Log errors
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
    print(f'Update {update} caused error {context.error}')


# Run the program
if __name__ == '__main__':
    app = Application.builder().token(TOKEN).build()

    # Commands
    app.add_handler(CommandHandler('start', start_command))
    app.add_handler(CommandHandler('help', help_command))
    app.add_handler(CommandHandler('custom', custom_command))

    # Messages
    app.add_handler(MessageHandler(filters.TEXT, handle_message))

    # Log all errors
    app.add_error_handler(error)

    print('Polling...')
    # Run the bot
    app.run_polling(poll_interval=5)

第二个代码是“responses.py”

import re


def process_message(message, response_array, responses):
    list_message = re.findall(r"[\w']  + | [.,?!;]", message.lower())
    score = 0
    for word in list_message:
        if word in response_array:
            score = score + 1
    
    print(score, responses)
    return [score, responses]


def get_response(message):
    response_list = [
        process_message(message, ['hello', 'hi', 'hey'], 'Hey there!'),
        process_message(message, ['how are you', 'how\'s it going', 'how\'s life'], 'I\'m doing well, thanks for asking!'),
        process_message(message, ['what is your name', 'who are you', 'your name'], 'My name is Bot, nice to meet you!'),
        process_message(message, ['what time is it', 'what\'s the time'], 'It\'s time to get a watch!'),
        process_message(message, ['what is your favorite color', 'favorite color'], 'My favorite color is blue.'),
        process_message(message, ['what is your favorite food', 'favorite food'], 'I don\'t eat food, sorry!'),
        process_message(message, ['thanks', 'thank you', 'thx'], 'You\'re welcome!'),
        process_message(message, ['goodbye']) 
    ]

我怎样才能合并这两个?

Instend 我需要摆脱小偷并在这里合并上面的代码

def handle_response(text: str) -> str:
        # Create your own response logic
        processed: str = text.lower()
    
        if 'hello' in processed:
            return 'Hey there!'
    
        if 'how are you' in processed:
            return 'I\'m good!'
    
        if 'i love python' in processed:
            return 'Remember to subscribe!'
    
        return 'I don\'t understand'

提前致谢

python python-3.x request python-re python-telegram-bot
© www.soinside.com 2019 - 2024. All rights reserved.