nonebot2/apiclient.py

101 lines
3.9 KiB
Python
Raw Normal View History

2016-12-03 22:41:12 +08:00
import os
2016-12-02 22:24:19 +08:00
import requests
class ApiClient:
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
2017-01-11 13:29:40 +08:00
def _api_url(self, via):
if via == 'qq':
return self.qq_api_url
elif via == 'wx':
return self.wx_api_url
return None
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
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
def send_friend_message(self, content: str, ctx_msg: dict):
2016-12-03 17:16:16 +08:00
try:
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:
pass
return None
2017-01-11 14:16:51 +08:00
def get_group_info(self, ctx_msg: dict):
url = self._api_url(ctx_msg.get('via'))
if url:
try:
return requests.get(url + '/get_group_info')
except requests.exceptions.ConnectionError:
return None
2016-12-02 22:24:19 +08:00
2017-01-11 14:16:51 +08:00
def get_user_info(self, ctx_msg: dict):
url = self._api_url(ctx_msg.get('via'))
2017-01-11 13:29:40 +08:00
if url:
try:
return requests.get(url + '/get_user_info')
except requests.exceptions.ConnectionError:
return None
2017-01-11 20:43:37 +08:00
def wx_consult(self, account, content):
if self.wx_api_url:
try:
return requests.get(self.wx_api_url + '/consult', params={'account': account, 'content': content})
except requests.exceptions.ConnectionError:
return None
2016-12-02 22:24:19 +08:00
client = ApiClient()