calculate.py 988 B

12345678910111213141516171819202122232425262728293031323334
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. async def command_calculate(room: MatrixRoom, event: RoomMessageText, bot):
  4. prompt = event.body.split()[2:]
  5. text = False
  6. results_only = True
  7. if "--text" in prompt:
  8. text = True
  9. delete = prompt.index("--text")
  10. del prompt[delete]
  11. if "--details" in prompt:
  12. results_only = False
  13. delete = prompt.index("--details")
  14. del prompt[delete]
  15. prompt = " ".join(prompt)
  16. if prompt:
  17. bot.logger.log("Querying WolframAlpha")
  18. for subpod in bot.calculation_api.generate_calculation_response(prompt, text, results_only):
  19. bot.logger.log(f"Sending subpod...")
  20. if isinstance(subpod, bytes):
  21. await bot.send_image(room, subpod)
  22. else:
  23. await bot.send_message(room, subpod, True)
  24. return
  25. await bot.send_message(room, "You need to provide a prompt.", True)