2016-12-03 22:41:12 +08:00
|
|
|
import os
|
|
|
|
|
2016-12-02 22:24:19 +08:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
class ApiClient:
|
2016-12-29 23:45:34 +08:00
|
|
|
qq_api_url = os.environ.get('QQ_API_URL')
|
|
|
|
wx_api_url = os.environ.get('WX_API_URL')
|
2016-12-02 22:24:19 +08:00
|
|
|
|
2016-12-30 22:01:50 +08:00
|
|
|
def send_message(self, content: str, ctx_msg: dict):
|
|
|
|
msg_type = ctx_msg.get('type')
|
|
|
|
if msg_type == 'group_message':
|
|
|
|
return self.send_group_message(content=content, ctx_msg=ctx_msg)
|
|
|
|
elif msg_type == 'discuss_message':
|
|
|
|
return self.send_discuss_message(content=content, ctx_msg=ctx_msg)
|
|
|
|
elif msg_type == 'friend_message':
|
|
|
|
return self.send_friend_message(content=content, ctx_msg=ctx_msg)
|
|
|
|
return None
|
|
|
|
|
2016-12-29 23:45:34 +08:00
|
|
|
def send_group_message(self, content: str, ctx_msg: dict):
|
|
|
|
try:
|
|
|
|
if ctx_msg.get('via') == 'qq' and self.qq_api_url:
|
|
|
|
params = {'content': content}
|
|
|
|
if ctx_msg.get('group_uid'):
|
|
|
|
params['uid'] = ctx_msg.get('group_uid')
|
|
|
|
elif ctx_msg.get('group_id'):
|
|
|
|
params['id'] = ctx_msg.get('group_id')
|
|
|
|
return requests.get(self.qq_api_url + '/send_group_message', params=params)
|
|
|
|
elif ctx_msg.get('via') == 'wx' and self.wx_api_url:
|
|
|
|
params = {'content': content}
|
|
|
|
if ctx_msg.get('group_id'):
|
|
|
|
params['id'] = ctx_msg.get('group_id')
|
|
|
|
return requests.get(self.wx_api_url + '/send_group_message', params=params)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
def send_discuss_message(self, content: str, ctx_msg: dict):
|
|
|
|
try:
|
|
|
|
if ctx_msg.get('via') == 'qq' and self.qq_api_url:
|
|
|
|
params = {'content': content}
|
|
|
|
if ctx_msg.get('discuss_id'):
|
|
|
|
params['id'] = ctx_msg.get('discuss_id')
|
|
|
|
return requests.get(self.qq_api_url + '/send_discuss_message', params=params)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
pass
|
|
|
|
return None
|
2016-12-02 22:24:19 +08:00
|
|
|
|
2016-12-29 23:45:34 +08:00
|
|
|
def send_friend_message(self, content: str, ctx_msg: dict):
|
2016-12-03 17:16:16 +08:00
|
|
|
try:
|
2016-12-29 23:45:34 +08:00
|
|
|
if ctx_msg.get('via') == 'qq' and self.qq_api_url:
|
|
|
|
params = {'content': content}
|
|
|
|
if ctx_msg.get('sender_uid'):
|
|
|
|
params['uid'] = ctx_msg.get('sender_uid')
|
|
|
|
elif ctx_msg.get('sender_id'):
|
|
|
|
params['id'] = ctx_msg.get('sender_id')
|
|
|
|
return requests.get(self.qq_api_url + '/send_friend_message', params=params)
|
|
|
|
elif ctx_msg.get('via') == 'wx' and self.wx_api_url:
|
|
|
|
params = {'content': content}
|
|
|
|
if ctx_msg.get('sender_account'):
|
|
|
|
params['account'] = ctx_msg.get('sender_account')
|
|
|
|
elif ctx_msg.get('sender_id'):
|
|
|
|
params['id'] = ctx_msg.get('sender_id')
|
|
|
|
return requests.get(self.wx_api_url + '/send_friend_message', params=params)
|
2016-12-03 17:16:16 +08:00
|
|
|
except requests.exceptions.ConnectionError:
|
2016-12-29 23:45:34 +08:00
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_group_info(self, via):
|
|
|
|
url = None
|
|
|
|
if via == 'qq':
|
|
|
|
url = self.qq_api_url
|
|
|
|
elif via == 'wx':
|
|
|
|
url = self.wx_api_url
|
|
|
|
if url:
|
|
|
|
try:
|
|
|
|
return requests.get(url + '/get_group_info')
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return None
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
|
2016-12-29 23:45:34 +08:00
|
|
|
client = ApiClient()
|