Python调用茉莉机器人API接口
生活随笔
收集整理的這篇文章主要介紹了
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.0import json import requestsconversation = "你好!" 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)效果展示
總結
以上是生活随笔為你收集整理的Python调用茉莉机器人API接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Java调用jdbc接口连接MySQL
- 下一篇: 单例模式和模版模式