emotibot.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. """
  3. Emotibot机器人API:http://www.emotibot.com
  4. """
  5. from __future__ import division
  6. from ..__load__ import *
  7. from .base import Base
  8. class Emotibot(Base):
  9. def __init__(self):
  10. super(self.__class__, self).__init__()
  11. def setting(self, appid, location, more, **args):
  12. self.appid, self.location, self.more = appid, location, more
  13. def chat(self, fp):
  14. msg = ''.join(texts)
  15. try:
  16. url = "http://idc.emotibot.com/api/ApiKey/openapi.php"
  17. userid = str(get_mac())[:32]
  18. register_data = {
  19. "cmd": "chat",
  20. "appid": self.appid,
  21. "userid": userid,
  22. "text": msg,
  23. "location": self.location
  24. }
  25. r = requests.post(url, params=register_data)
  26. jsondata = json.loads(r.text)
  27. result = ''
  28. responds = []
  29. if jsondata['return'] == 0:
  30. if self.more:
  31. datas = jsondata.get('data')
  32. for data in datas:
  33. if data.get('type') == 'text':
  34. responds.append(data.get('value'))
  35. else:
  36. responds.append(jsondata.get('data')[0].get('value'))
  37. result = '\n'.join(responds)
  38. else:
  39. result = "抱歉, 我的大脑短路了,请稍后再试试."
  40. return self.chatLog(1, result)
  41. except Exception:
  42. return self.chatLog(2, msg)