直接上程序。。
# coding:utf-8
import itchat
import urllib
import os
import webbrowser
import json
from itchat.content import TEXT
import pyttsx
from itchat.content import *
from aip import AipSpeech
import codecs
import paho.mqtt.client as mqtt
import mp3play
import sys
from pydub import AudioSegment
import time
import re
reload(sys)
sys.setdefaultencoding('utf8')
import os
APP_ID = '百度给的ID'
API_KEY = '百度API_KEY'
SECRET_KEY = '百度 SECRET_KEY'
# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
key = '图灵给的key'
api = 'h t t p : / / w w w .t u l i n g 1 2 3 . c o m /openapi/api?key=' + key + '&info='
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def on_connect(client,userdata,password, rc):
print("Connected with result code " + str(rc))
msg_information = {}
face_bug=None #针对表情包的内容
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
@itchat.msg_register([TEXT, RECORDING, ATTACHMENT],isFriendChat=True)
def handle_receive_msg(msg):
names = itchat.search_friends(userName=msg['FromUserName'])['NickName']
usera = itchat.search_friends(userName=msg['FromUserName'])
nameb = usera['UserName']
users = itchat.search_friends(name='雨电')
namea = users[0]['UserName']
print names
print namea
print nameb
global face_bug
msg_time_rec = 1022 #接受消息的时间
# msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] #在好友列表中查询发送信息的好友昵称
msg_time = msg['CreateTime'] #信息发送的时间
msg_id = msg['MsgId'] #每条信息的id
msg_content = None #储存信息的内容
# msg_share_url = None #储存分享的链接,比如分享的**和音乐
# print msg['Type']
# print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': #接受文字信息
msg_content = msg['Text']
print msg_content
info=msg_content
if(names == '雨电'): #判断发信息人 能否控制读取物联网硬件 这里我做了两个环境监测一个放家里一个放店里
if (info.find('家里') !=-1 ):
os.system(r'E:\dioyong\pobao1.py')
engine = pyttsx.init()
engine.say(u'家里环境监测完毕')
engine.runAndWait()
print u'家里环境监测完毕'
elif (info.find('店里') !=-1 ):
os.system(r'E:\dioyong\pobao.py')
engine = pyttsx.init()
engine.say(u'店铺环境监测完毕')
engine.runAndWait()
print u'店铺环境监测完毕'
elif (info.find('打开') !=-1 ): #做了一个灯控硬件
os.system(r'E:\dioyong\dakai.py')
engine = pyttsx.init()
engine.say(u'以为你打开灯')
engine.runAndWait()
print u'以为你打开灯'
elif (info.find('拍照') !=-1 ): #做的两个拍照截取图片 一个是海康的监控摄像头 一个是USB摄像头
os.system(r'E:\dioyong\cap.py')
os.system(r'E:\dioyong\haikang.py')
itchat.send_image("cap.png", namea)
itchat.send_image("haikang.jpg", namea)
elif (info.find('关闭') !=-1):
os.system(r'E:\dioyong\guanbi.py')
engine = pyttsx.init()
engine.say(u'以为你关闭灯')
engine.runAndWait()
print u'以为你关闭灯'
elif (info.find('店里环境') ==-1 ):
request = api + info.encode("utf8")
response = getHtml(request)
dic_json = json.loads(response)
if dic_json['code']==100000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),namea) #图灵返回的的文字信息
elif dic_json['code'] == 200000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),namea) #图灵返回有单个链接信息
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['url'].decode() , namea)
# webbrowser.open_new(dic_json['url'])
elif dic_json['code'] == 302000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),namea) #图灵返回有多个链接信息
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , namea)
# webbrowser.open_new(b)
a =a+1
elif dic_json['code'] == 308000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),namea)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , namea)
# webbrowser.open_new(b)
a =a+1
f = codecs.open(r'E:\dioyong\wenxin.txt', "w",'utf-8')
f.write(dic_json['text'])
result = aipSpeech.synthesis(dic_json['text'], 'zh', 1, {'spd': 4,'pit': 5,
'vol': 5,'per':0,
})
if not isinstance(result, dict):
with open(r'E:\dioyong\auido2.mp3', 'wb') as f:
f.write(result)
clip = mp3play.load(r'E:\dioyong\auido2.mp3')
clip.play()
time.sleep(min(3000000, clip.seconds()))
os.remove(r'E:\dioyong\auido2.mp3')
else:
request = api + info.encode("utf8")
response = getHtml(request)
dic_json = json.loads(response)
if dic_json['code']==100000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
elif dic_json['code'] == 200000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['url'].decode() , nameb)
# webbrowser.open_new(dic_json['url'])
elif dic_json['code'] == 302000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , nameb)
# webbrowser.open_new(b)
a =a+1
elif dic_json['code'] == 308000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , nameb)
# webbrowser.open_new(b)
a =a+1
#如果发送的消息是附件、视屏、图片、语音
elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ #接受朋友发的语音
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
msg_content = msg['FileName']
msg['Text'](msg_content)
print msg_content
sound = AudioSegment.from_mp3(msg_content)
sound.export(r'02.wav' , format = "wav")
result = aipSpeech.asr(get_file_content(r'02.wav'), 'wav', 8000, {
'lan': 'zh',
})
print result['result'][0]
info=result['result'][0]
if(names == '雨电'):
if (info.find('家里') !=-1 ):
os.system(r'E:\dioyong\pobao1.py')
engine = pyttsx.init()
engine.say(u'家里环境监测完毕')
engine.runAndWait()
print u'家里环境监测完毕'
elif (info.find('店里') !=-1 ):
os.system(r'E:\dioyong\pobao.py')
engine = pyttsx.init()
engine.say(u'店铺环境监测完毕')
engine.runAndWait()
print u'店铺环境监测完毕'
elif (info.find('打开') !=-1 ):
os.system(r'E:\dioyong\dakai.py')
engine = pyttsx.init()
engine.say(u'以为你打开灯')
engine.runAndWait()
print u'以为你打开灯'
elif (info.find('拍照') !=-1 ):
os.system(r'E:\dioyong\cap.py')
os.system(r'E:\dioyong\haikang.py')
itchat.send_image("cap.png", namea)
itchat.send_image("haikang.jpg", namea)
elif (info.find('关闭') !=-1):
os.system(r'E:\dioyong\guanbi.py')
engine = pyttsx.init()
engine.say(u'以为你关闭灯')
engine.runAndWait()
print u'以为你关闭灯'
elif (info.find('店里环境') ==-1 ):
request = api + info.encode("utf8")
response = getHtml(request)
dic_json = json.loads(response)
if dic_json['code']==100000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode() , namea)
elif dic_json['code'] == 200000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode() , namea)
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['url'] , namea)
# webbrowser.open_new(dic_json['url'])
elif dic_json['code'] == 302000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode() , namea)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , namea)
# webbrowser.open_new(b)
a =a+1
elif dic_json['code'] == 308000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode() , namea)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , namea)
# webbrowser.open_new(b)
a =a+1
f = codecs.open(r'E:\dioyong\wenxin.txt', "w",'utf-8')
f.write(dic_json['text'])
result = aipSpeech.synthesis(dic_json['text'], 'zh', 1, {'spd': 4,'pit': 5,
'vol': 5,'per':0,
})
if not isinstance(result, dict):
with open(r'E:\dioyong\auido2.mp3', 'wb') as f:
f.write(result)
clip = mp3play.load(r'E:\dioyong\auido2.mp3')
clip.play()
time.sleep(min(3000000, clip.seconds()))
os.remove(r'E:\dioyong\auido2.mp3')
else:
request = api + info.encode("utf8")
response = getHtml(request)
dic_json = json.loads(response)
if dic_json['code']==100000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
elif dic_json['code'] == 200000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['url'] , nameb)
# webbrowser.open_new(dic_json['url'])
elif dic_json['code'] == 302000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , nameb)
# webbrowser.open_new(b)
a =a+1
elif dic_json['code'] == 308000:
print '机器人: '.decode('utf-8') + dic_json['text']
itchat.send_msg('机器人小雨: '.decode('utf-8') +dic_json['text'].decode(),nameb)
lis = dic_json['list']
a=0
while True:
if a==2:
break
else:
b= lis[a]['detailurl']
itchat.send_msg('机器人小雨: '.decode('utf-8') +b , nameb)
# webbrowser.open_new(b)
a =a+1
itchat.auto_login(hotReload=True)
itchat.send_msg()
itchat.run()
本来还想通过mqtt客户端接受到检测信号,让微信机器人主动给某个好友或几个好友发送信息的。。用以做报警或预警用的 一直没弄得起来mqtt客户端(要保持长期运行)没办法和微信机器人(也要保持长期运行)程序同时运行。。。由于本人能力不足。试了多进程和变量共享都没有成功。。。。希望有大神帮忙指点指点。
|
|