преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
1234567891011121314151617181920212223242526
  1. # encoding:utf-8
  2. import requests
  3. from bot.bot import Bot
  4. # Baidu Unit对话接口 (可用, 但能力较弱)
  5. class BaiduUnitBot(Bot):
  6. def reply(self, query, context=None):
  7. token = self.get_token()
  8. url = 'https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=' + token
  9. 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}}}"
  10. print(post_data)
  11. headers = {'content-type': 'application/x-www-form-urlencoded'}
  12. response = requests.post(url, data=post_data.encode(), headers=headers)
  13. if response:
  14. return response.json()['result']['context']['SYS_PRESUMED_HIST'][1]
  15. def get_token(self):
  16. access_key = 'wdNwpvzh4fBr8xzTuPePOWhU'
  17. secret_key = 'EUtLXyrogfTQV8nnjncnqNEfeyoHDUqS'
  18. host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + access_key + '&client_secret=' + secret_key
  19. response = requests.get(host)
  20. if response:
  21. print(response.json())
  22. return response.json()['access_token']