Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from twitchio.ext import commands
|
| 3 |
+
import asyncio
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Replace these with your Twitch credentials
|
| 8 |
+
TWITCH_TOKEN = 'your_twitch_token'
|
| 9 |
+
TWITCH_CLIENT_ID = 'your_client_id'
|
| 10 |
+
TWITCH_CHANNEL = 'target_twitch_channel'
|
| 11 |
+
|
| 12 |
+
# Initialize Twitch Bot
|
| 13 |
+
bot = commands.Bot(
|
| 14 |
+
token=TWITCH_TOKEN,
|
| 15 |
+
client_id=TWITCH_CLIENT_ID,
|
| 16 |
+
prefix='!',
|
| 17 |
+
initial_channels=[TWITCH_CHANNEL]
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# This will store messages received from Twitch chat temporarily
|
| 21 |
+
# Consider a thread-safe implementation or a persistent storage for scalability
|
| 22 |
+
messages_from_twitch = []
|
| 23 |
+
|
| 24 |
+
@app.before_first_request
|
| 25 |
+
def start_bot():
|
| 26 |
+
# Run the bot in the background
|
| 27 |
+
asyncio.new_event_loop().run_until_complete(bot.start())
|
| 28 |
+
|
| 29 |
+
@bot.event
|
| 30 |
+
async def event_message(ctx):
|
| 31 |
+
# Don't let the bot reply to itself
|
| 32 |
+
if ctx.author.name.lower() == bot.nick.lower():
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
print(f'Message from {ctx.author.name}: {ctx.content}')
|
| 36 |
+
# Append messages to the global list
|
| 37 |
+
messages_from_twitch.append({'user': ctx.author.name, 'message': ctx.content})
|
| 38 |
+
|
| 39 |
+
@app.route('/sendMessage', methods=['POST'])
|
| 40 |
+
def send_message():
|
| 41 |
+
# Get message from SecondLife and send it to Twitch chat
|
| 42 |
+
message = request.data.decode()
|
| 43 |
+
asyncio.run(bot.get_channel(TWITCH_CHANNEL).send(message))
|
| 44 |
+
return jsonify({'status': 'success', 'message': 'Message sent to Twitch chat'}), 200
|
| 45 |
+
|
| 46 |
+
@app.route('/getMessages', methods=['GET'])
|
| 47 |
+
def get_messages():
|
| 48 |
+
# Return messages received from Twitch and clear the list
|
| 49 |
+
global messages_from_twitch
|
| 50 |
+
msgs = messages_from_twitch.copy()
|
| 51 |
+
messages_from_twitch.clear()
|
| 52 |
+
return jsonify(msgs), 200
|
| 53 |
+
|
| 54 |
+
if __name__ == '__main__':
|
| 55 |
+
app.run(debug=True)
|