mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-03 00:40:45 +08:00
init: build minimum viable version
This commit is contained in:
BIN
bot/__pycache__/bot.cpython-36.pyc
Normal file
BIN
bot/__pycache__/bot.cpython-36.pyc
Normal file
Binary file not shown.
BIN
bot/__pycache__/bot_factory.cpython-36.pyc
Normal file
BIN
bot/__pycache__/bot_factory.cpython-36.pyc
Normal file
Binary file not shown.
BIN
bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc
Normal file
BIN
bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc
Normal file
Binary file not shown.
26
bot/baidu/baidu_unit_bot.py
Normal file
26
bot/baidu/baidu_unit_bot.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# encoding:utf-8
|
||||
|
||||
import json
|
||||
import requests
|
||||
from bot.bot import Bot
|
||||
|
||||
|
||||
class BaiduUnitBot(Bot):
|
||||
def reply(self, query):
|
||||
token = self.get_token()
|
||||
url = 'https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=' + token
|
||||
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}}}"
|
||||
print(post_data)
|
||||
headers = {'content-type': 'application/x-www-form-urlencoded'}
|
||||
response = requests.post(url, data=post_data.encode(), headers=headers)
|
||||
if response:
|
||||
return response.json()['result']['context']['SYS_PRESUMED_HIST'][1]
|
||||
|
||||
def get_token(self):
|
||||
access_key = '${YOUR_ACCESS_KEY}'
|
||||
secret_key = '${YOUR_SECRET_KEY}'
|
||||
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + access_key + '&client_secret=' + secret_key
|
||||
response = requests.get(host)
|
||||
if response:
|
||||
print(response.json())
|
||||
return response.json()['access_token']
|
||||
13
bot/bot.py
Normal file
13
bot/bot.py
Normal file
@@ -0,0 +1,13 @@
|
||||
"""
|
||||
Auto-replay chat robot abstract class
|
||||
"""
|
||||
|
||||
|
||||
class Bot(object):
|
||||
def reply(self, query):
|
||||
"""
|
||||
bot auto-reply content
|
||||
:param req: received message
|
||||
:return: reply content
|
||||
"""
|
||||
raise NotImplementedError
|
||||
16
bot/bot_factory.py
Normal file
16
bot/bot_factory.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
channel factory
|
||||
"""
|
||||
|
||||
from bot.baidu.baidu_unit_bot import BaiduUnitBot
|
||||
|
||||
|
||||
def create_bot(bot_type):
|
||||
"""
|
||||
create a channel instance
|
||||
:param channel_type: channel type code
|
||||
:return: channel instance
|
||||
"""
|
||||
if bot_type == 'baidu':
|
||||
return BaiduUnitBot()
|
||||
raise RuntimeError
|
||||
Reference in New Issue
Block a user