qq聊天机器人
第一步去申请一个图灵机器人。
http://www.tuling123.com
- 我用的python的版本是是python3.6
- 两个代码放在同一个文件夹里。
- 需要导包啥的,可以直接在pycharm里面下载,我就不演示了,这里我直接贴上我的代码
- 每次运行的时候需要手机扫码登录,可以把代码运行在远程的服务器,就可以实现一天到晚QQ机器人都开着
tulingapi.py
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 
 | #!/usr/bin/python# -*- coding: utf-8 -*-
 import json, urllib, urllib.parse, urllib.request
 header = {
 "User-Agent": 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36',
 "Content-Type": 'text/html; charset=utf-8'}
 
 
 def main(text='哈哈哈',userid="123456"):
 # 配置您申请的APPKey
 appkey = "e75a3b457d4b48eabea084b1b1006a2a"
 
 # 1.问答
 return request1(appkey, "GET", text, userid)
 
 
 # 问答
 def request1(appkey, m="GET", text="哈哈", userid="123456"):
 url = "http://www.tuling123.com/openapi/api"
 params = {
 "key": appkey,  # 您申请到的本接口专用的APPKEY
 "info": text,  # 要发送给机器人的内容,不要超过30个字符
 "userid": userid   #自定义唯一 userid(1-32位,字母与数字组成),可以识别上下文
 }
 params = urllib.parse.urlencode(params)
 if m == "GET":
 req = urllib.request.Request("%s?%s" % (url, params), data=None, headers=header)
 f = urllib.request.urlopen(req)
 else:
 req = urllib.request.Request(url + params, data=None, headers=header)
 f = urllib.request.urlopen(req)
 
 content = f.read().decode()
 res = json.loads(content)
 print("我的req是:  ",end="  ")
 print(appkey, text)
 print("我的返回结果", end="  :")
 print(res)
 if res:
 error_code = res["code"]
 if error_code != 40001 and error_code != 40002:
 # 成功请求
 response = res['text']
 if 'url'in res:
 response += "\n"+res['url']
 if 'list' in res:
 for li in res['list']:
 if "article" in li:
 response += "\n"+li['article']
 if "source" in li:
 response += "\n"+li['source']
 if "info" in li:
 response += "\n" + li['info']
 if "detailurl" in li:
 response += "\n"+li['detailurl']
 pass
 return response
 else:
 err = json.dumps(res)
 with open('log.txt', 'a+') as oo:
 oo.write(err + "\n\r" + text)
 # print ("%s:%s" % (res["error_code"],res["reason"]))
 return "我好像出错了$%$*%*&^#$@#%$@#%"
 else:
 return "我的接口好像出错了,哈哈哈,创造我的人技术不行啊"
 
 | 
第二个代码是
qq.py
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | from qqbot import QQBotSlot as qqbotslot,RunBotimport tulingapi
 
 #使用的python版本是python3.6
 # 初始化图灵机器人
 friend = []
 
 @qqbotslot
 def onQQMessage(bot, contact, member, content):
 if contact.ctype == 'group':
 pass
 elif contact.ctype == 'buddy' and "--stop" == content:
 if contact.mark in friend:
 friend.remove(contact.mark)
 bot.SendTo(contact, '关闭机器人')
 elif contact.ctype == 'buddy' and "--start" == content and '哈哈,哈哈哈,哈哈哈哈哈' != contact.mark:
 if contact.mark not in friend:
 friend.append(contact.mark)
 bot.SendTo(contact, '欢迎使用机器人')
 elif contact.ctype == 'buddy' and contact.mark in friend:                        #对指定的人发自动回复
 daan = tulingapi.main(content)
 bot.SendTo(contact, '🍅'+daan)
 print(contact.name, contact.ctype, contact.nick, contact.uin, contact.mark)
 elif 'mememeda' == content:
 print("关闭机器人")
 bot.Stop()
 
 
 
 RunBot()
 
 | 
最后的效果是当QQ好友给你发一个–start的时候开启机器人然后就可以自动回复了(多个好友可以同时自动回复),当对方给你回一个–stop的时候就停止机器人聊天,当你对任何人或者任何人给你发送mememeda的时候把这个程序给彻底关掉。