main.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. from urllib.parse import urlparse
  3. import os
  4. import time
  5. import asyncio
  6. import simplematrixbotlib as botlib
  7. import nio
  8. from speech_recognition import ASR
  9. creds = botlib.Creds(
  10. homeserver=os.environ['HOMESERVER'],
  11. username=os.environ['USERNAME'],
  12. password=os.getenv('PASSWORD', None),
  13. login_token=os.getenv('LOGIN_TOKEN', None),
  14. access_token=os.getenv('ACCESS_TOKEN', None),
  15. session_stored_file="/data/session.txt"
  16. )
  17. config = botlib.Config()
  18. config.encryption_enabled = True
  19. config.emoji_verify = False
  20. config.ignore_unverified_devices = True
  21. config.store_path = '/data/crypto_store/'
  22. if 'ALLOWLIST' in os.environ:
  23. config.allowlist = os.environ['ALLOWLIST'].split(',')
  24. bot = botlib.Bot(creds, config)
  25. asr = ASR(os.getenv('ASR_MODEL', os.getenv('PRELOAD_MODEL', 'tiny')), os.getenv('ASR_LANGUAGE', 'en'))
  26. @bot.listener.on_custom_event(nio.RoomMessage)
  27. async def on_message(room, event):
  28. if not isinstance(event, (nio.RoomMessageAudio,
  29. nio.RoomEncryptedAudio,
  30. nio.RoomMessageVideo,
  31. nio.RoomEncryptedVideo)):
  32. return
  33. encrypted = isinstance(event, (nio.RoomEncryptedAudio, nio.RoomEncryptedVideo))
  34. print(room.machine_name, event.sender, event.body, event.url)
  35. match = botlib.MessageMatch(room, event, bot)
  36. if match.is_not_from_this_bot():
  37. await bot.async_client.room_typing(room.machine_name, True, timeout=120000)
  38. url = urlparse(event.url)
  39. response = await bot.async_client.download(server_name=url.netloc, media_id=url.path[1:])
  40. if encrypted:
  41. print("decrypting...")
  42. data = nio.crypto.attachments.decrypt_attachment(
  43. response.body,
  44. event.source["content"]["file"]["key"]["k"],
  45. event.source["content"]["file"]["hashes"]["sha256"],
  46. event.source["content"]["file"]["iv"],
  47. )
  48. else:
  49. data = response.body
  50. print(response)
  51. result = await asr.transcribe(data)
  52. await bot.async_client.room_typing(room.machine_name, False)
  53. if not result:
  54. print("No result")
  55. return
  56. filename = response.filename or event.body
  57. if filename:
  58. reply = f"Transcription of {filename}: {result}"
  59. else:
  60. reply = f"Transcription: {result}"
  61. await bot.api._send_room(
  62. room_id=room.room_id,
  63. content={
  64. "msgtype": "m.notice",
  65. "body": reply,
  66. "m.relates_to": {
  67. "m.in_reply_to": {
  68. "event_id": event.event_id
  69. }
  70. }
  71. })
  72. if __name__ == "__main__":
  73. asr.load_model()
  74. try:
  75. bot.run()
  76. except asyncio.exceptions.TimeoutError as e:
  77. print(e)
  78. print("Timeout, restarting...")
  79. time.sleep(5)