не знаю работает или нет, мне его предоставили, по задумке должен работать теллеграм бот в связке с сервером, проверить возможности нету
Python:
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
class Server:
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('localhost', 12345))
self.server.listen(5)
def run(self):
while True:
client, addr = self.server.accept()
data = client.recv(1024).decode()
print(f'Message received: {data}')
client.send('Message received'.encode())
client.close()
def reload_server_thread():
while True:
time.sleep(72 * 3600)
# Add code here to reload the server
def process_message(message):
# Add code here to process the message using the machine learning model
response = 'Response from AI model'
return response
def message_handler_bot(update, context):
message = update.message.text
context.bot.send_message(chat_id=update.message.chat_id, text='Response from bot')
def message_handler_ai(update, context):
message = update.message.text
response = process_message(message)
context.bot.send_message(chat_id=update.message.chat_id, text=response)
def start_command(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Hello, I'm a bot. How can I help?")
# Create an instance of the server and start it in a separate thread
server = Server()
server_thread = threading.Thread(target=server.run)
server_thread.daemon = True
server_thread.start()
# Start a thread to periodically reload the server every 72 hours
reload_server_thread = threading.Thread(target=reload_server_thread)
reload_server_thread.daemon = True
reload_server_thread.start()
# Setting up the bot and command/message handlers
updater = Updater(token='YOUR_BOT_TOKEN', use_context=True)
dp = updater.dispatcher
# Adding command and message handlers
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, message_handler_ai))
# Start the bot
updater.start_polling()
updater.idle()
Последнее редактирование: