当前位置:   article > 正文

给天猫精灵赋予chatgpt_天猫精灵接入chatgpt

天猫精灵接入chatgpt

前言

自从chatgpt火了之后,一直想把智障的天猫精灵对接个chatgpt,奈何天猫精灵不提供接口,终于在github上找到大佬做的小爱同学对接chatgpt的教程,于是根据他的原理,也搞出了个简陋的版本

成功展示

 开发中遇到的问题

1.获取天猫精灵的对话记录,卡了我好几天,采取的frida远程调用,参数都对,就检验不通过,原来是转义字符的问题,还有没注意到变量重复赋值。

2.天猫的接口文档自我感觉也是一套糊涂,写的很粗糙。可能我能力不够吧。

3.chatgpt用的都是镜像站,一不注意,免费的就收费了,引流太严重啊。

部分代码展示

  1. # -*- coding: utf-8 -*-
  2. # This file is auto-generated, don't edit it. Thanks.
  3. import asyncio
  4. import sys
  5. from alibabacloud_aligenieiap_1_0.client import Client as AliGenieiap_1_0Client
  6. from alibabacloud_aligenieiap_1_0.models import PushNotificationsRequestNotificationUnicastRequest, \
  7. PushNotificationsRequestTenantInfo, PushNotificationsRequestNotificationUnicastRequestSendTarget
  8. from alibabacloud_tea_openapi import models as open_api_models
  9. from alibabacloud_aligenieiap_1_0 import models as ali_genieiap__1__0_models
  10. import requests
  11. import json
  12. from urllib import parse
  13. import frida
  14. import time
  15. import datetime
  16. last_timestamp = int(time.time())
  17. class Aligenie:
  18. @staticmethod
  19. def create_client(
  20. access_key_id: str,
  21. access_key_secret: str,
  22. ) -> AliGenieiap_1_0Client:
  23. """
  24. 使用AK&SK初始化账号Client
  25. @param access_key_id:
  26. @param access_key_secret:
  27. @return: Client
  28. @throws Exception
  29. """
  30. config = open_api_models.Config(
  31. # 您的AccessKey ID,
  32. access_key_id=access_key_id,
  33. # 您的AccessKey Secret,
  34. access_key_secret=access_key_secret
  35. )
  36. # 访问的域名
  37. config.endpoint = 'openapi.aligenie.com'
  38. return AliGenieiap_1_0Client(config)
  39. @staticmethod
  40. def push_message(content):
  41. client = Aligenie.create_client('access_key_id', 'access_key_secret')
  42. notificationUnicastRequest = PushNotificationsRequestNotificationUnicastRequest()
  43. notificationUnicastRequest.encode_key = "98223"
  44. notificationUnicastRequest.encode_type = "SKILL_ID"
  45. notificationUnicastRequest.organization_id = "1398082853812549793"
  46. place_holder = {'content': content}
  47. notificationUnicastRequest.place_holder = place_holder
  48. notificationUnicastRequest.message_template_id = "2DPRcMi97Qiwofsj"
  49. notificationUnicastRequest.is_debug = True
  50. requestSendTarget = PushNotificationsRequestNotificationUnicastRequestSendTarget()
  51. requestSendTarget.target_type = 'DEVICE_OPEN_ID'
  52. requestSendTarget.target_identity = 'JqyRCF91nuWRCTdk5dbOKVjRWMRBzL2jjI+xkIG3U2gLVoH5nvRw9w=='
  53. notificationUnicastRequest.send_target = requestSendTarget
  54. requestTenantInfo = PushNotificationsRequestTenantInfo()
  55. push_notifications_request = ali_genieiap__1__0_models.PushNotificationsRequest(notificationUnicastRequest,
  56. requestTenantInfo)
  57. try:
  58. response = client.push_notifications(push_notifications_request)
  59. print(response)
  60. except Exception as err:
  61. print(err)
  62. class Chat:
  63. @staticmethod
  64. def one(content):
  65. #换成自己的chatgpt或者自己找免费的接口
  66. pass
  67. @staticmethod
  68. def two(content):
  69. #换成自己的chatgpt或者自己找免费的接口
  70. pass
  71. class AliGenieFrida:
  72. def __init__(self):
  73. self.last_timestamp = int(time.time())
  74. def get_frida_rpc_script(self):
  75. js = open('tmjl.js', 'r', encoding='utf-8').read()
  76. session = frida.get_usb_device().attach("com.alibaba.ailabs.tg")
  77. script = session.create_script(js)
  78. script.load()
  79. return script
  80. def get_data(self):
  81. for _ in range(2):
  82. try:
  83. #换成抓包的data
  84. data = '{"limit":"10","uuid":"uuid","deviceInfo":"{' \
  85. '\\"bizGroup\\":\\"X1\\",' \
  86. '\\"bizType\\":\\"AILABS\\",\\"botId\\":10,' \
  87. '\\"uuid\\":\\"uuid\\"}",' \
  88. '"authInfo":"{\\"accessToken\\":\\"2zyAfPN+bJdMzh5dUtiWqDtLLL/KnlYJCjsMujjz' \
  89. '+xYeTwqhbS2JvHGWAg7K' \
  90. '+nTyip' \
  91. '/vBXf83wZXe+wBdmHUWqs8OhxFYToW\\",\\"deviceIds\\":[],\\"isAuthenticated\\":false,' \
  92. '\\"isNew\\":true,' \
  93. '\\"isTempUser\\":false,\\"userId\\":\\"userId\\",' \
  94. '\\"utdId\\":\\"ZGOM/fF/pVkDADEloBB6B5tx\\"}"}'
  95. api = 'mtop.alibaba.aicloud.index.listsentences'
  96. appKey = '23904773'
  97. timestamp = str(int(time.time()))
  98. res = json.loads(self.get_frida_rpc_script().exports.callgetsign(data, timestamp, api, appKey))
  99. res['timestamp'] = timestamp
  100. res['x-sgext'] = parse.quote(res['x-sgext'])
  101. res['x-mini-wua'] = parse.quote(res['x-mini-wua'])
  102. res['x-sign'] = parse.quote(res['x-sign'])
  103. res['x-t'] = parse.quote(res['timestamp'])
  104. requests.get("http://43.142.90.87:8610/rs?key=sign&vaule=" + json.dumps(res) + "&time_out=100")
  105. # print("调用frida获取", res)
  106. headers = {
  107. 'x-sid': 'sid',
  108. 'x-uid': 'uid',
  109. 'x-nettype': 'WIFI',
  110. 'x-pv': '6.3',
  111. 'x-nq': 'WIFI',
  112. 'x-features': '27',
  113. 'x-app-conf-v': '0',
  114. 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  115. 'cache-control': 'no-cache',
  116. 'x-t': '1684334633',
  117. 'x-bx-version': '6.5.24',
  118. 'f-refer': 'mtop',
  119. 'x-extdata': 'openappkey%3DDEFAULT_AUTH',
  120. 'x-ttid': '702008%40genieapp_android_6.0.1',
  121. 'x-app-ver': '6.0.1',
  122. 'x-c-traceid': 'ZGOM%2FfF%2FpVkDADEloBB6B5tx1684334633416073015885',
  123. 'x-umt': 'GI8Bdk5LPPgZIwKIJIE1O6HP%2F11f01e2',
  124. 'x-utdid': 'ZGOM%2FfF%2FpVkDADEloBB6B5tx',
  125. 'x-appkey': '23904773',
  126. 'x-devid': 'Amqv8wxkiFMLQwGHcw6DOrRUeg7B3s693f8iah0kXC6E',
  127. 'user-agent': 'MTOPSDK%2F3.1.1.7+%28Android%3B8.1.0%3BGoogle%3BPixel%29',
  128. 'Host': 'acs.m.taobao.com',
  129. }
  130. headers['x-sgext'] = res['x-sgext']
  131. headers['x-mini-wua'] = res['x-mini-wua']
  132. headers['x-sign'] = res['x-sign']
  133. headers['x-t'] = res['timestamp']
  134. params = {
  135. 'data': data,
  136. }
  137. response = requests.get(
  138. 'https://acs.m.taobao.com/gw/mtop.alibaba.aicloud.index.listsentences/1.0/',
  139. params=params,
  140. headers=headers,
  141. ).json()
  142. # print(response)
  143. for _ in response['data']['model']:
  144. if _['query'] != '' and _['cateName'] == '百科':
  145. return _
  146. return response['data']['model'][0]
  147. except Exception as e:
  148. print(e)
  149. async def print_every_two_seconds():
  150. while True:
  151. aliGenieFrida = AliGenieFrida()
  152. qa = aliGenieFrida.get_data()
  153. query = qa['query']
  154. reply = qa['reply']
  155. queryTime = qa['queryTime']
  156. last = int(time.mktime(datetime.datetime.strptime(queryTime, '%Y-%m-%d %H:%M:%S.%f').timetuple()))
  157. global last_timestamp
  158. print(last_timestamp, query, last)
  159. if query != '' and last > last_timestamp:
  160. last_timestamp = last
  161. content = Chat.two(query)
  162. print(f"问题:{query},天猫精灵的回答:{reply},chat的回答:{content}")
  163. Aligenie.push_message(content)
  164. await asyncio.sleep(2)
  165. async def main():
  166. task = asyncio.create_task(print_every_two_seconds())
  167. await task
  168. if __name__ == '__main__':
  169. asyncio.run(main())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/71358
推荐阅读
相关标签
  

闽ICP备14008679号