Python调用茉莉机器人API接口

使用工具

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: 206647497@qq.com
# @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)

效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值