51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import asyncio
|
|||
|
|
import sys
|
||
|
|
from nio import AsyncClient, LoginResponse, RoomSendResponse
|
||
|
|
from room import Config
|
||
|
|
|
||
|
|
async def main():
|
||
|
|
if len(sys.argv) < 2 or len(sys.argv) > 4:
|
||
|
|
print("Usage: python3 simulate_suggestions.py simulation_suggestions.txt [delay_seconds] [config_path]")
|
||
|
|
return
|
||
|
|
|
||
|
|
file_path = sys.argv[1]
|
||
|
|
delay = float(sys.argv[2]) if len(sys.argv) >= 3 else 5.0
|
||
|
|
config_path = sys.argv[3] if len(sys.argv) == 4 else "configs/config.yaml"
|
||
|
|
|
||
|
|
with open(file_path, "r") as f:
|
||
|
|
suggestions = [line.strip() for line in f if line.strip()]
|
||
|
|
|
||
|
|
if not suggestions:
|
||
|
|
print("No suggestions found in file.")
|
||
|
|
return
|
||
|
|
|
||
|
|
config = Config(config_path)
|
||
|
|
client = AsyncClient(config.homeserver, config.username)
|
||
|
|
resp = await client.login(config.password)
|
||
|
|
if not isinstance(resp, LoginResponse):
|
||
|
|
print(f"Failed to log in: {resp}")
|
||
|
|
return
|
||
|
|
|
||
|
|
print(f"Posting {len(suggestions)} suggestions to {config.room_id} "
|
||
|
|
f"(every {delay:g}s to avoid rate limits)...")
|
||
|
|
for i, suggestion in enumerate(suggestions, start=1):
|
||
|
|
content = {
|
||
|
|
"msgtype": "m.text",
|
||
|
|
"body": f"!suggest {suggestion}",
|
||
|
|
}
|
||
|
|
resp = await client.room_send(
|
||
|
|
config.room_id,
|
||
|
|
message_type="m.room.message",
|
||
|
|
content=content,
|
||
|
|
)
|
||
|
|
if isinstance(resp, RoomSendResponse):
|
||
|
|
print(f"Posted {i}/{len(suggestions)}: {suggestion}")
|
||
|
|
else:
|
||
|
|
print(f"Failed to post suggestion {i}: {resp}")
|
||
|
|
await asyncio.sleep(delay)
|
||
|
|
|
||
|
|
await client.close()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
asyncio.run(main())
|