added discord helper.py
This commit is contained in:
@@ -1,19 +1,8 @@
|
|||||||
import asyncio
|
from src.discord_helper import send_message
|
||||||
import sys
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
from src.discord_helper import run_discord, discord_client, send_message
|
|
||||||
from src.mail.mail_confirmation import read_mails_and_find_confirmation_contacts
|
from src.mail.mail_confirmation import read_mails_and_find_confirmation_contacts
|
||||||
from src.pojo import ReserveResultPojo
|
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):
|
def create_message_from_item(item: ReserveResultPojo):
|
||||||
return f"Phone: {item.phone}\n" \
|
return f"Phone: {item.phone}\n" \
|
||||||
f"URL: {item.url}\n" \
|
f"URL: {item.url}\n" \
|
||||||
@@ -22,20 +11,15 @@ def create_message_from_item(item: ReserveResultPojo):
|
|||||||
f"Last Name: {item.last_name}\n"
|
f"Last Name: {item.last_name}\n"
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
# initialize discord
|
# initialize discord
|
||||||
_thread = init_discord()
|
|
||||||
print("init discord done")
|
print("init discord done")
|
||||||
# wait for discord to start up
|
# wait for discord to start up
|
||||||
# time.sleep(10)
|
# time.sleep(10)
|
||||||
_accepted_appointments = read_mails_and_find_confirmation_contacts()
|
_accepted_appointments = read_mails_and_find_confirmation_contacts()
|
||||||
if _accepted_appointments is not None and len(_accepted_appointments) > 0:
|
|
||||||
for item in _accepted_appointments:
|
for item in _accepted_appointments:
|
||||||
asyncio.run_coroutine_threadsafe(send_message(create_message_from_item(item)), discord_client.loop)
|
send_message(create_message_from_item(item))
|
||||||
_thread.join()
|
|
||||||
else:
|
|
||||||
raise Exception("Stop processing message")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
|||||||
+8
-32
@@ -1,38 +1,14 @@
|
|||||||
# This example requires the 'message_content' intent.
|
import requests
|
||||||
import asyncio
|
|
||||||
|
|
||||||
import discord
|
DISCORD_SERVER_URL = "http://127.0.0.1:9000/discord/send"
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
|
||||||
intents.messages = True
|
|
||||||
|
|
||||||
discord_client = discord.Client(intents=intents)
|
|
||||||
|
|
||||||
|
|
||||||
@discord_client.event
|
def send_message(message: str):
|
||||||
async def on_ready():
|
headers = {"Authorization": "Bearer 97e36f7e-340e-4c02-b329-9415faee38c3", "Content-Type": "application/json"}
|
||||||
print(f'We have logged in as {discord_client.user}')
|
message_data = {"message": message}
|
||||||
# await send_message("我是机器人")
|
_res = requests.post(DISCORD_SERVER_URL, json=message_data, headers=headers)
|
||||||
|
print(_res.text)
|
||||||
|
|
||||||
@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)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# loop = asyncio.get_event_loop()
|
send_message('测试')
|
||||||
# loop.run_until_complete(client.start())
|
|
||||||
# client.start()
|
|
||||||
asyncio.run(run_discord())
|
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user