使用工具 PyCharm 2018.2.3 茉莉机器人官网地址:http://www.itpk.cn/ 准备步骤 先在茉莉机器人官网注册一个账号 代码示例 Main.py #_*_coding:utf-8_*_ # @Author: VVcat # @Time: 2019/7/22 12:27 # @Fil
使用工具
PyCharm 2018.2.3
茉莉机器人官网地址:http://www.itpk.cn/
准备步骤
先在茉莉机器人官网注册一个账号
代码示例
Main.py
#_*_coding:utf-8_*_# @Author: VVcat
# @Time: 2019/7/22 12:27
# @File: Main.py
# @IDE: PyCharm
# @Email:
# @Version: 1.0
import json
import requests
conversation = "你好!"
tuling = "VVcat:"
robot = "LoCat:"
while True: # 完成对话的循环过程
moli_data = {
"question": conversation, # 将消息转为json 进行数据交互
"api_key": "这里请输入官方获取的api_key", # 例 "api_key": "xxxxxxxxxxxxxxxx",
"api_secret": "这里请输入官方获取的api_secret" # 例 "api_secret": "xxxxxxxxxxxxxxxx"
}
moli_api_url = 'http://i.itpk.cn/api.php' # 官方提供的接口
return_json = requests.post(moli_api_url, data=moli_data) # 将返回回来的Json数据进行解析
if '笑话' == conversation:
if return_json.text.startswith(u'\ufeff'):
str = return_json.text.encode('utf8')[3:].decode('utf8')
json_change_wordbook = json.loads(str)
title = json_change_wordbook['title']
content = json_change_wordbook['content']
print(robot + title + "\n" + content)
elif '观音灵签' == conversation:
if return_json.text.startswith(u'\ufeff'):
str = return_json.text.encode('utf8')[3:].decode('utf8')
json_change_wordbook = json.loads(str)
number1 = json_change_wordbook['number1']
number2 = json_change_wordbook['number2']
haohua = json_change_wordbook['haohua']
qianyu = json_change_wordbook['qianyu']
shiyi = json_change_wordbook['shiyi']
jieqian = json_change_wordbook['jieqian']
type = json_change_wordbook['type']
print(number1 + "\n" + number2 + "\n" + haohua + "\n" + qianyu + "\n" + shiyi + "\n" + jieqian + "\n" + type)
elif '月老灵签' == conversation:
if return_json.text.startswith(u'\ufeff'):
str = return_json.text.encode('utf8')[3:].decode('utf8')
json_change_wordbook = json.loads(str)
number1 = json_change_wordbook['number1']
number2 = json_change_wordbook['number2']
haohua = json_change_wordbook['haohua']
shiyi = json_change_wordbook['shiyi']
jieqian = json_change_wordbook['jieqian']
zhushi = json_change_wordbook['zhushi']
baihua = json_change_wordbook['baihua']
type = json_change_wordbook['type']
print(number1 + "\n" + number2 + "\n" + haohua + "\n" + shiyi + "\n" + jieqian + "\n" + zhushi + "\n" + baihua + "\n" + type)
elif '财神爷灵签' == conversation:
if return_json.text.startswith(u'\ufeff'):
str = return_json.text.encode('utf8')[3:].decode('utf8')
json_change_wordbook = json.loads(str)
number1 = json_change_wordbook['number1']
number2 = json_change_wordbook['number2']
qianyu = json_change_wordbook['qianyu']
zhushi = json_change_wordbook['zhushi']
jieqian = json_change_wordbook['jieqian']
jieshuo = json_change_wordbook['jieshuo']
jieguo = json_change_wordbook['jieguo']
hunyin = json_change_wordbook['hunyin']
jiaoyi = json_change_wordbook['jiaoyi']
type = json_change_wordbook['type']
print(number1 + "\n" + number2 + "\n" + qianyu + "\n" + zhushi + "\n" + jieqian + "\n" + jieshuo + "\n" + jieguo + "\n" + hunyin + "\n" + jiaoyi + "\n" + type)
else:
print(robot + return_json.text)
conversation = input(tuling)
效果展示