Guest User

Untitled

a guest
Dec 6th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. import telebot
  2. import requests
  3. import json
  4. import re
  5. from googletrans import Translator
  6.  
  7. bot = telebot.TeleBot('') # bot id
  8.  
  9. arr = []
  10. trs = []
  11. x1 = ''
  12. x2 = ''
  13. x3 = ''
  14. x4 = ''
  15. data = ''
  16. out = ''
  17. final = ''
  18. final2 = ''
  19. final3 = ''
  20. final4 = ''
  21. msg_id = ''
  22. msg_id2 = ''
  23. msg_id3 = ''
  24. msg_id4 = ''
  25.  
  26. headers = {
  27. 'authority': 'beta.character.ai',
  28. 'accept': '*/*',
  29. 'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6',
  30. 'authorization': '', # token
  31. 'content-type': 'application/json',
  32. 'dnt': '1',
  33. 'origin': 'https://beta.character.ai',
  34. 'referer': 'https://beta.character.ai/chat?char=',
  35. 'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
  36. 'sec-ch-ua-mobile': '?0',
  37. 'sec-ch-ua-platform': '"Windows"',
  38. 'sec-fetch-dest': 'empty',
  39. 'sec-fetch-mode': 'cors',
  40. 'sec-fetch-site': 'same-origin',
  41. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
  42. }
  43.  
  44. json_data = {
  45. 'history_external_id': '', # history id
  46. 'character_external_id': '', # character id
  47. 'text': '',
  48. 'tgt': '', #internal id
  49. 'ranking_method': 'random',
  50. 'staging': False,
  51. 'model_server_address': None,
  52. 'override_prefix': None,
  53. 'override_rank': None,
  54. 'rank_candidates': None,
  55. 'filter_candidates': None,
  56. 'prefix_limit': None,
  57. 'prefix_token_limit': None,
  58. 'livetune_coeff': None,
  59. 'stream_params': None,
  60. 'enable_tti': True,
  61. 'initial_timeout': None,
  62. 'insert_beginning': None,
  63. 'translate_candidates': None,
  64. 'stream_every_n_steps': 100,
  65. 'chunks_to_pad': 100,
  66. 'is_proactive': False,
  67. 'image_rel_path': '',
  68. 'image_description': '',
  69. 'image_description_type': '',
  70. 'image_origin_type': '',
  71. 'voice_enabled': False,
  72. }
  73. update_headers = {
  74. 'authority': 'beta.character.ai',
  75. 'accept': '*/*',
  76. 'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6',
  77. 'authorization': '', # token
  78. 'content-type': 'application/json',
  79. 'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
  80. 'sec-ch-ua-mobile': '?0',
  81. 'sec-ch-ua-platform': '"Windows"',
  82. 'sec-fetch-dest': 'empty',
  83. 'sec-fetch-mode': 'cors',
  84. 'sec-fetch-site': 'same-origin',
  85. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
  86. }
  87. update_json_data = {
  88. 'message_id': '',
  89. 'reason': 'SWIPE',
  90. }
  91.  
  92. def send_msg():
  93.  
  94. global arr
  95. global trs
  96. global x1,x2,x3,x4
  97. global final,final2,final3,final4
  98. global msg_id
  99.  
  100. translator = Translator()
  101.  
  102. response = requests.post('https://beta.character.ai/chat/streaming/', headers=headers, json=json_data)
  103.  
  104. print('Response:', response.status_code)
  105. f = response.text
  106. log = open('response.txt', 'w')
  107. log.write(f)
  108. log.close()
  109.  
  110. for matched in re.findall(r'{\"text\": \"(.*?)}', f):
  111. arr.append(matched)
  112. str = '{"text": "' + arr[-1] + '}'
  113. str2 = '{"text": "' + arr[-2] + '}'
  114. str3 = '{"text": "' + arr[-3] + '}'
  115. str4 = '{"text": "' + arr[-4] + '}'
  116. try:
  117. data = json.loads(str)
  118. except Exception as e:
  119. print("Some error occured while loading json")
  120. try:
  121. data2 = json.loads(str2)
  122. except Exception as e:
  123. print("Some error occured while loading json")
  124. try:
  125. data3 = json.loads(str3)
  126. except Exception as e:
  127. print("Some error occured while loading json")
  128. try:
  129. data4 = json.loads(str4)
  130. except Exception as e:
  131. print("Some error occured while loading json")
  132. try:
  133. final = data['text']
  134. msg_id = data['id']
  135. except Exception as e:
  136. print("Some error occured while parsing json")
  137. try:
  138. final2 = data2['text']
  139. except Exception as e:
  140. print("Some error occured while parsing json")
  141. try:
  142. final3 = data3['text']
  143. except Exception as e:
  144. print("Some error occured while parsing json")
  145. try:
  146. final4 = data4['text']
  147. except Exception as e:
  148. print("Some error occured while parsing json")
  149.  
  150. if (msg_id!=''):
  151. update_json_data["message_id"] = msg_id
  152.  
  153. response_update = requests.post('https://beta.character.ai/chat/msg/update/primary/', headers=update_headers, json=update_json_data)
  154. print('Response:', response_update.text)
  155. print(msg_id)
  156.  
  157.  
  158. @bot.message_handler(content_types=["text"])
  159. def handle_text(message):
  160. global json_data
  161. global x1,x2,x3,x4
  162. global out
  163. json_data["text"] = message.text
  164. print('Message: ', message.text)
  165.  
  166. send_msg()
  167. try:
  168. bot.send_message(message.chat.id,final)
  169. except Exception as e:
  170. print("Some error occured while sending message")
  171.  
  172. bot.polling(none_stop=True, interval=0, timeout=600)
Advertisement
Add Comment
Please, Sign In to add comment