client.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. from godot import exposed, export
  2. from godot import *
  3. from threading import Thread
  4. from time import sleep
  5. from nio import HttpClient
  6. from nio import Api, LoginError, RegisterResponse, JoinError, RoomVisibility, LoginResponse
  7. from nio import MessageDirection, RoomMessagesResponse, RoomMessagesError
  8. import requests
  9. import asyncio
  10. from queue import SimpleQueue
  11. from nio import Api, events
  12. class WorkerThread(Thread):
  13. def __init__(self,_mc):
  14. super().__init__()
  15. self.mc = _mc
  16. self.room_name = ''
  17. print("initting the thread")
  18. def run(self):
  19. do_work( self.mc, self.room_name )
  20. def do_work(mc, room_name):
  21. print("worker is invoked")
  22. while True:
  23. mc.fetch_new_messages(room_name)
  24. sleep(0.1)
  25. #print("still running, yeah")
  26. @exposed
  27. class MatrixClient(Node2D):
  28. # member variables here, example:
  29. a = export(int)
  30. thread = False
  31. homeserver = export(str, default='some.server')
  32. username = ''
  33. client: HttpClient = None
  34. syncing = False
  35. token = False
  36. batch = 0
  37. q: SimpleQueue = None
  38. def _ready(self):
  39. print("hello, godot-python")
  40. self.thread = WorkerThread(self)
  41. self.homeserver = str(self.homeserver)
  42. self.username = str(self.username)
  43. print(self.homeserver, self.username)
  44. self.client = HttpClient(self.homeserver, self.username)
  45. self.q = SimpleQueue()
  46. #thread.start()
  47. def register(self, password):
  48. base_url = self.homeserver
  49. (_ignored, path, data) = Api.register(self.username,str(password), 'GODOT')
  50. result =requests.post(base_url+path,data= data, verify=False)
  51. print(result)
  52. if result.status_code != 200:
  53. return False
  54. return True
  55. def login(self, password):
  56. self.client.connect()
  57. result = self.client.login(str(password))
  58. print(result)
  59. if isinstance(result, LoginError):
  60. return False
  61. return True
  62. def login2(self, password):
  63. base_url = self.homeserver
  64. (_ignored, path, data) = Api.login(self.username,str(password), 'GODOT')
  65. result =requests.post(base_url+path,data= data, verify=False)
  66. self.token = result.json()["access_token"]
  67. print("token ", self.token)
  68. if result.status_code != 200:
  69. return False
  70. return True
  71. def create_if_not(self, _room_name):
  72. room_name = str(_room_name)
  73. create_res = self.client.room_create(
  74. RoomVisibility.public,
  75. alias=room_name,
  76. name=room_name)
  77. print("creation result", create_res)
  78. def create_if_not2(self, _room_name):
  79. room_name = str(_room_name)
  80. base_url = self.homeserver
  81. (_ignored, path, data) = Api.room_create(
  82. self.token,
  83. RoomVisibility.public,
  84. alias=room_name,
  85. name=room_name)
  86. result =requests.post(base_url+path,data= data, verify=False)
  87. print("creation result", result.json())
  88. if result.status_code != 200 and not 'IN_USE' in result.json()['errcode']:
  89. return False
  90. return True
  91. def resolve(self, room_alias):
  92. pass
  93. def join(self, room_id_or_alias):
  94. room_name = str(_room_name)
  95. joinres= client.join(room_alias)
  96. print(joinres)
  97. if isinstance(new_joinres, JoinError):
  98. return False
  99. return True
  100. def join2(self, room_id_or_alias):
  101. room_name = str(room_id_or_alias)
  102. base_url = self.homeserver
  103. (_ignored, path, data) = Api.join(
  104. self.token,room_name)
  105. result =requests.post(base_url+path,data= data, verify=False)
  106. print("join result", result, result.json())
  107. print(result.status_code)
  108. if result.status_code != 200:
  109. return False
  110. return result.json()['room_id']
  111. def get_first_batch(self):
  112. print("sending a sync")
  113. base_url = self.homeserver
  114. (_ignored, path) = Api.sync(self.token)
  115. result =requests.get(base_url+path, verify=False)
  116. print("sync result", result)
  117. if result.status_code != 200:
  118. return False
  119. self.batch = result.json()["next_batch"]
  120. def start_listen(self, room_id):
  121. self.thread.room_name = room_id
  122. self.thread.start()
  123. def fetch_new_messages(self,room_id):
  124. room_name = str(room_id)
  125. base_url = self.homeserver
  126. (_ignored, path) = Api.room_messages(self.token, room_name, self.batch, direction=MessageDirection.front)
  127. result =requests.get(base_url+path, verify=False)
  128. #print("messages result", result, result.json().keys())
  129. rmr: RoomMessagesResponse = RoomMessagesResponse.from_dict(result.json(), room_id)
  130. if isinstance(rmr, RoomMessagesError):
  131. return False
  132. self.batch = rmr.end
  133. print ("batch ", self.batch)
  134. for e in rmr.chunk:
  135. #print ("received event of type ", e.source['type'])
  136. if e.source['type'] != 'm.room.message':
  137. continue
  138. room_msg: RoomMessageText = events.Event.parse_event(e.source)
  139. print("received message ", room_msg.body)
  140. self.q.put(room_msg.body)
  141. if result.status_code != 200:
  142. return False
  143. return True
  144. def fetch_one(self):
  145. if not self.q.empty():
  146. return self.q.get()
  147. else:
  148. return ''