from flask import Flask, request, jsonify from twitchio.ext import commands import asyncio app = Flask(__name__) # Replace these with your Twitch credentials TWITCH_TOKEN = 'your_twitch_token' TWITCH_CLIENT_ID = 'your_client_id' TWITCH_CHANNEL = 'target_twitch_channel' # Initialize Twitch Bot bot = commands.Bot( token=TWITCH_TOKEN, client_id=TWITCH_CLIENT_ID, prefix='!', initial_channels=[TWITCH_CHANNEL] ) # This will store messages received from Twitch chat temporarily # Consider a thread-safe implementation or a persistent storage for scalability messages_from_twitch = [] @app.before_first_request def start_bot(): # Run the bot in the background asyncio.new_event_loop().run_until_complete(bot.start()) @bot.event async def event_message(ctx): # Don't let the bot reply to itself if ctx.author.name.lower() == bot.nick.lower(): return print(f'Message from {ctx.author.name}: {ctx.content}') # Append messages to the global list messages_from_twitch.append({'user': ctx.author.name, 'message': ctx.content}) @app.route('/sendMessage', methods=['POST']) def send_message(): # Get message from SecondLife and send it to Twitch chat message = request.data.decode() asyncio.run(bot.get_channel(TWITCH_CHANNEL).send(message)) return jsonify({'status': 'success', 'message': 'Message sent to Twitch chat'}), 200 @app.route('/getMessages', methods=['GET']) def get_messages(): # Return messages received from Twitch and clear the list global messages_from_twitch msgs = messages_from_twitch.copy() messages_from_twitch.clear() return jsonify(msgs), 200 if __name__ == '__main__': app.run(debug=True)