Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

29 linhas
1.3KB

  1. # encoding:utf-8
  2. import requests
  3. from bot.bot import Bot
  4. from bridge.reply import Reply, ReplyType
  5. # Baidu Unit对话接口 (可用, 但能力较弱)
  6. class BaiduUnitBot(Bot):
  7. def reply(self, query, context=None):
  8. token = self.get_token()
  9. url = 'https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=' + token
  10. post_data = "{\"version\":\"3.0\",\"service_id\":\"S73177\",\"session_id\":\"\",\"log_id\":\"7758521\",\"skill_ids\":[\"1221886\"],\"request\":{\"terminal_id\":\"88888\",\"query\":\"" + query + "\", \"hyper_params\": {\"chat_custom_bot_profile\": 1}}}"
  11. print(post_data)
  12. headers = {'content-type': 'application/x-www-form-urlencoded'}
  13. response = requests.post(url, data=post_data.encode(), headers=headers)
  14. if response:
  15. reply = Reply(ReplyType.TEXT, response.json()['result']['context']['SYS_PRESUMED_HIST'][1])
  16. return reply
  17. def get_token(self):
  18. access_key = 'YOUR_ACCESS_KEY'
  19. secret_key = 'YOUR_SECRET_KEY'
  20. host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + access_key + '&client_secret=' + secret_key
  21. response = requests.get(host)
  22. if response:
  23. print(response.json())
  24. return response.json()['access_token']