File size: 1,704 Bytes
09746c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)