python 微信图灵聊天机器人

from wxpy import *
import requests
from threading import Timer

tulingKey = "d31debb8a4104eb592abb2d87a1ad6a0"
itchat = Bot(cache_path="botoo.pkl")
friend = itchat.friends().search(name = u'灵月')

tuling = Tuling(api_key=tulingKey)
def getNews():
    url = "http://open.iciba.com/dsapi/"
    r = requests.get(url)
    content = r.json()['content']
    note = r.json()['note']
    return content, note
#tuling自动回复的消息注册

def reply(msg):
    return tuling.do_reply(msg)

#接受到消息后的打印
@itchat.register(msg_types=TEXT)
def printMsg(msg):  
    print(msg)
    print('tuling自动回复消息---'+reply(msg))
    
#发送每日一句消息函数
def sendNews():
    try:
        #这里是备注print


        content = getNews()

        message1 = str(content[0])
        message2 = str(content[1])
        print('--------------今日内容:-------------\n'+message1+'\n'+message2+"\n---------------------------------")
        # message3 = "进行最后一项测试embed()"
        print('----当前friend对象为:'+str(friend))

        for index,item in enumerate(friend):
            rightMark = True
            while rightMark:
                print("---------------将要发送给 "+str(item)+" ing,index="+str(index)+' 是否发送?')
                order = input("-----------------请输入指令(1是2否3退出当前操作循环):")
                order = int(order)
                if(order == 1):
                    rightMark = False
                    item.send(message1)
                    item.send(message2)
                    print("----------消息发送成功----------------")

                elif(order ==2):
                    rightMark = False
                    print("----------消息发送操作被忽略-----------")
                    continue
                elif(order ==3):
                    rightMark = False
                    print("----------消息发送程序退出------------")
                    break
                else:
                    print("-------指令错误,请重新输入!")           
    except:
        errorMessage = "BugBugBug!"
        for index,item in enumerate(friend):
            item.send(errorMessage)

def sendMessage():
    name = input("请输入好友备注:")
    friend = itchat.friends().search(name = name)
    if(len(friend)>0):
        print("找寻到备注为"+name+"的好友\n"+str(friend)+"\n如果存在多个该备注好友,默认选择第一个人发送")
        sendMessageByFriend(friend[0])
    else:
        print("未找到备注为:"+name+"的好友!")

def sendMessageByFriend(friend):


    rightMark = True
    while rightMark:
        mess = input("------请输入您要发的信息:")
        order = input("----------即将发送的信息为:"+mess+" 请选择是否发送 0是1否--------")
        order = int(order)
        if(order == 0):
            friend.send(mess)
            print("----------消息已成功发送(注意此处无catch)-------")
            rightMark = False
        elif(order ==1):
            print("----------消息未发送--------")
            rightMark = False
        else:
            print("---------指令错误,请重新输入!")




if __name__ == "__main__":
    # sendNews()
    embed()

首先,你需要注册一个图灵账号,api_key就是从中获取的,
之后,安装Python模块。
friend = itchat.friends().search(name = u’灵月’)
name是好友的name
当指定的好友给你发消息时,按下enter就会自动回复。

python 微信图灵聊天机器人_第1张图片

# coding:utf-8
import json  
import requests  
from wxpy import *  

def reply(text):  
    url = "http://www.tuling123.com/openapi/api"  
    api_key="d31debb8a4104eb592abb2d87a1ad6a0"
    payload={  
            "key":api_key,  
            "info":text,  
            "userid":"666"  
            }  
    r = requests.post(url,data=json.dumps(payload))  
    result = json.loads(r.content)  
    if ('url' in result.keys()):  
        return ""+result["text"]+result["url"]  
    else:  
        return ""+result["text"]  


bot = Bot(cache_path=True) #登录缓存  
print(u'robot上线')  
found = bot.friends().search(u'灵月')

print(found)

@bot.register(found)

def message(msg): 
    ret = reply(msg.text)  
    return ret

@bot.register(found)
def forward_message(msg):  
    ret = reply(msg.text)  
    return ret 
embed()

#这个更简洁,而且可以自动回复

你可能感兴趣的:(python,微信开发)