added discord helper.py

This commit is contained in:
2024-12-09 22:04:18 +01:00
parent a5b698f267
commit bb34ffebb6
3 changed files with 13 additions and 53 deletions
+5 -21
View File
@@ -1,19 +1,8 @@
import asyncio
import sys
import threading
import time
from src.discord_helper import run_discord, discord_client, send_message
from src.discord_helper import send_message
from src.mail.mail_confirmation import read_mails_and_find_confirmation_contacts
from src.pojo import ReserveResultPojo
def init_discord():
thread = threading.Thread(target=run_discord)
thread.start()
return thread
def create_message_from_item(item: ReserveResultPojo):
return f"Phone: {item.phone}\n" \
f"URL: {item.url}\n" \
@@ -22,20 +11,15 @@ def create_message_from_item(item: ReserveResultPojo):
f"Last Name: {item.last_name}\n"
async def main():
def main():
# initialize discord
_thread = init_discord()
print("init discord done")
# wait for discord to start up
# time.sleep(10)
_accepted_appointments = read_mails_and_find_confirmation_contacts()
if _accepted_appointments is not None and len(_accepted_appointments) > 0:
for item in _accepted_appointments:
asyncio.run_coroutine_threadsafe(send_message(create_message_from_item(item)), discord_client.loop)
_thread.join()
else:
raise Exception("Stop processing message")
for item in _accepted_appointments:
send_message(create_message_from_item(item))
if __name__ == '__main__':
asyncio.run(main())
main()
+8 -32
View File
@@ -1,38 +1,14 @@
# This example requires the 'message_content' intent.
import asyncio
import requests
import discord
intents = discord.Intents.default()
intents.messages = True
discord_client = discord.Client(intents=intents)
DISCORD_SERVER_URL = "http://127.0.0.1:9000/discord/send"
@discord_client.event
async def on_ready():
print(f'We have logged in as {discord_client.user}')
# await send_message("我是机器人")
@discord_client.event
async def on_message(message):
pass
async def send_message(message):
print("will call send")
channel = discord_client.get_channel(1149373754053496866)
await channel.send(message)
def run_discord():
_token = 'MTMxNTI5OTkxODk4MTg5MDExOQ.GKA6lA.wHM4fTFT1luVrptK4YxEtFn_WhUnavtIrla_TE'
discord_client.run(_token)
def send_message(message: str):
headers = {"Authorization": "Bearer 97e36f7e-340e-4c02-b329-9415faee38c3", "Content-Type": "application/json"}
message_data = {"message": message}
_res = requests.post(DISCORD_SERVER_URL, json=message_data, headers=headers)
print(_res.text)
if __name__ == '__main__':
# loop = asyncio.get_event_loop()
# loop.run_until_complete(client.start())
# client.start()
asyncio.run(run_discord())
send_message('测试')
Binary file not shown.