The Interface of the fdb-spider, based on rocketchat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.5 KiB

  1. import os
  2. from rocketchat.api import RocketChatAPI
  3. import configparser
  4. import json
  5. import requests
  6. config = configparser.ConfigParser()
  7. config.read('config.ini')
  8. botname = config['Chat']['username']
  9. botpassword = config['Chat']['password']
  10. server_url = config['Chat']['URL']
  11. room_id = config['Chat']['room_id']
  12. bot_user_id = config['Chat']['bot_user_id']
  13. spider_directory = config['Spider']['spider_directory']
  14. # here comes the functions to talk to gpt
  15. # For local streaming, the websockets are hosted without ssl - http://
  16. HOST = 'localhost:5000'
  17. URI = f'http://{HOST}/api/v1/chat'
  18. #URI = f'http://{HOST}/api'
  19. # http://192.168.9.197:5000/api/v1/chat
  20. # For reverse-proxied streaming, the remote will likely host with ssl - https://
  21. # URI = 'https://your-uri-here.trycloudflare.com/api/v1/chat'
  22. if __name__ == '__main__':
  23. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  24. # api.send_message('Ciao, I am the fdb-spider', room_id)
  25. # myinfo = api.get_my_info()
  26. # print(myinfo)
  27. # rooms = api.get_private_rooms()
  28. # print(rooms)
  29. # api.send_message('Ole', room_id)
  30. n = 0
  31. import time
  32. import schedule
  33. def getroomdata():
  34. room_list = []
  35. rooms = os.listdir('rooms')
  36. for room in rooms:
  37. room_file = open('rooms/' + room, 'r')
  38. room_file_raw = room_file.read()
  39. room_file.close()
  40. room_file_list = room_file_raw.split('§%§%')
  41. room_list.append([room[:-3], room_file_list[0], room_file_list[1], room_file_list[2]])
  42. return room_list
  43. import datetime
  44. from datetime import timedelta
  45. while True:
  46. time.sleep(4)
  47. n += 1
  48. print(n)
  49. try:
  50. #print('getting the room history')
  51. date = datetime.datetime.now() - timedelta(days=3)
  52. room_history = api.get_private_room_history(room_id, oldest=date)
  53. except Exception as e:
  54. time.sleep(10)
  55. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  56. time.sleep(5)
  57. room_history = api.get_private_room_history(room_id, oldest=date)
  58. print('got a connection error, original message is:',e)
  59. messages_list = []
  60. for message in room_history['messages']:
  61. messages_list.append(message)
  62. if len(messages_list) >= 1:
  63. #print('blub', messages_list)
  64. latest_message_user_id = messages_list[0]['u']['_id']
  65. latest_message_user_username = messages_list[0]['u']['username']
  66. latest_message = messages_list[0]['msg']
  67. latest_message_id = messages_list[0]['_id']
  68. new_message_file = open('new_message_file.txt', 'r')
  69. new_message = new_message_file.read()
  70. new_message_file.close()
  71. new_message_list = new_message.split('§%§%')
  72. #print(latest_message, new_message_list[0])
  73. if new_message_list[0] != latest_message and new_message_list[1] != latest_message_id and latest_message_user_id != bot_user_id:
  74. answer = 'Ich habe kein Kommando erhalten.'
  75. new_message_file = open('new_message_file.txt', 'w')
  76. new_message_file.write(latest_message + '§%§%' + latest_message_id)
  77. new_message_file.close()
  78. user_input = latest_message
  79. user_input_list = user_input.split(' ')
  80. if user_input_list[0] == 'printrooms':
  81. # get all the rooms
  82. import os
  83. room_list = []
  84. rooms = os.listdir('rooms')
  85. for room in rooms:
  86. room_file = open('rooms/' + room, 'r')
  87. room_file_raw = room_file.read()
  88. room_file.close()
  89. room_file_list = room_file_raw.split('§%§%')
  90. room_list.append([room[:-3], room_file_list[0], room_file_list[1], room_file_list[2]])
  91. #print(room_list)
  92. answer = str(room_list)
  93. if user_input_list[0] == 'createroom':
  94. try:
  95. room_name = user_input_list[1]
  96. except Exception as e:
  97. room_name = 'NONE'
  98. print(e)
  99. try:
  100. databases = user_input_list[2]
  101. except Exception as e:
  102. databases = 'NONE'
  103. print(e)
  104. try:
  105. filters = user_input_list[3]
  106. except Exception as e:
  107. filters = 'NONE'
  108. print(e)
  109. if len(user_input_list) > 3:
  110. answer = 'Die Syntax hat einen Fehler. Wahrscheinlich sind Leerzeichen in den Listen. Leerzeichen definieren die Syntax. Versuche es erneut, nach dem Muster -> createroom room1 ["fdb1","fdb2"] ["tag1","tag2"]'
  111. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE':
  112. answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags>'
  113. else:
  114. new_room = api.create_public_room(room_name,
  115. members=[latest_message_user_username],
  116. read_only=True)
  117. new_room_id = new_room['channel']['_id']
  118. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  119. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters)
  120. room_file.close()
  121. answer = 'Der Command wurde übermittelt'
  122. #print('oi', user_input)
  123. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  124. # here comes the code interacting with the spiders output json
  125. #answer = 'the up to date entries are: oi Oi Oi!'
  126. api.send_message(answer, room_id)
  127. time.sleep(1)
  128. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  129. time.sleep(1)