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.

45 line
1.5KB

  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 = (
  10. "https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token="
  11. + token
  12. )
  13. post_data = (
  14. '{"version":"3.0","service_id":"S73177","session_id":"","log_id":"7758521","skill_ids":["1221886"],"request":{"terminal_id":"88888","query":"'
  15. + query
  16. + '", "hyper_params": {"chat_custom_bot_profile": 1}}}'
  17. )
  18. print(post_data)
  19. headers = {"content-type": "application/x-www-form-urlencoded"}
  20. response = requests.post(url, data=post_data.encode(), headers=headers)
  21. if response:
  22. reply = Reply(
  23. ReplyType.TEXT,
  24. response.json()["result"]["context"]["SYS_PRESUMED_HIST"][1],
  25. )
  26. return reply
  27. def get_token(self):
  28. access_key = "YOUR_ACCESS_KEY"
  29. secret_key = "YOUR_SECRET_KEY"
  30. host = (
  31. "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="
  32. + access_key
  33. + "&client_secret="
  34. + secret_key
  35. )
  36. response = requests.get(host)
  37. if response:
  38. print(response.json())
  39. return response.json()["access_token"]