summaryrefslogtreecommitdiff
path: root/index.py
blob: 59a90c4ff234c0d68f3d2896175594db16a81e04 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request
import requests

app = Flask(__name__)

# Set up the necessary variables
token = "MTE0MTM0MDA2Mzk2ODU0MjczMA.GX75jG.frFFekrXjqk1BrAuTftSYClt1ERQn8BY2FiZgg"
channel_id = '1140989222556274721'

# Set the API endpoint
url = f'https://discord.com/api/v9/channels/{channel_id}/messages'

# Set the headers with the bot token for authentication
header = {'Authorization': f'Bot {token}'}

old_message = ""

@app.route('/', methods=['POST'])
def handle_post_request():
    global old_message
    dbug= {
        'content': "cao"
    }
    requests.post(url, headers=header, data=dbug)#DEBUGGGG=======
    message = request.get_data().decode('utf-8')
    requests.post(url, headers=header, data=dbug)#DEBUGGGG======= 

    isFile = False
    if message.split(maxsplit = 1)[0] == "FILE":
        isFile = True

    # Check if the message is different from the previous one
    requests.post(url, headers=header, data=dbug)#DEBUGGGG======= 

    if message != old_message:
        payload = {
            'content': message
        }
        if isFile:
            file = eval(message.split(maxsplit=1)[1])
            payload = {
                'content': f"``file: {file[0]}``"
            }
            try:
                files = {
                    "file" : (file[0],file[1].decode()) # The picture that we want to send in binary
                }
            except UnicodeDecodeError:
                files = {
                    "file" : (file[0],file[1]) # The picture that we want to send in binary
                }

            requests.post(url, headers=header, data=dbug)#DEBUGGGG======= 

            response = requests.post(url, headers=header, data=payload, files=files)
            requests.post(url, headers=header, data=dbug)#DEBUGGGG======= 

        else:
            response = requests.post(url, headers=header, data=payload)
        if response.status_code == 200:
            old_message = message
            return 'Message sent to Discord channel successfully.'
        else:
            return f'Error sending message to Discord channel: {response.status_code} - {response.text}'
    else:
        return 'Duplicate message. Not sending to Discord channel.'

if __name__ == '__main__':
    app.run()