import requests
import json

def send_message(token, channel_id, content):
    # token = input('input token: ')
    # content = input('input content: ')
    # channel_id = input('input channel id: ')
    payload = {
        'content': content
    }
    header = {
        'authorization': token
    }
    r = requests.post(f"https://discordapp.com/api/v6/channels/{channel_id}/messages", data=payload, headers=header)

def retreive_messages(token, channel_id):
    messages = []
    header = {
        'authorization': token
    }
    r = requests.get(f'https://discordapp.com/api/v6/channels/{channel_id}/messages', headers=header)
    json_object = json.loads(r.text)
    for value in json_object:
        if value['content'] == '':
            retreived_message = (value['author'])['username'] + ': undefined'
        else:
            retreived_message = ((value['author'])['username'] + ': ' + value['content'])
        messages.insert(0, retreived_message)
    for message in messages:
        print(message)
    
test_server = '845727539930071084' # test server
nine_T = '773276736594771999' # 9T chat

channel_id = nine_T
token = ''


# send_message(token, channel_id, 'hello world')
retreive_messages(token, channel_id)
