美文网首页
微信聊天机器人

微信聊天机器人

作者: 催化剂 | 来源:发表于2019-03-06 16:47 被阅读0次

import itchat

import requests

def get_response(msg):

apiUrl ='http://www.tuling123.com/openapi/api'  #改成你自己的图灵机器人的api

    data={

'key':'1d889b1679024d10893254dee2f3fe49',  # Tuling Key

        'info': msg,  # 这是我们发出去的消息

        'userid':'墨道',  # 这里可随意修改

    }

# 通过如下命令发送一个post请求

    r = requests.post(apiUrl, data=data).json()

print(r.get('text'))

return r.get('text')

#用于接收个人信息

@itchat.msg_register([itchat.content.TEXT,itchat.content.PICTURE,itchat.content.RECORDING,itchat.content.ATTACHMENT,itchat.content.VIDEO,itchat.content.MAP,itchat.content.VOICE,itchat.content.ATTACHMENT,itchat.content.NOTE,itchat.content.CARD],isFriendChat=True)

def print_content(msg):

user = msg['User']

groupNickName = user['NickName']

groupId = user['UserName']

msg_id = msg['MsgId']

msg_from_user = msg['User']['NickName']

msg_content = msg['Content']

msg_create_time = msg['CreateTime']

msg_type = msg['Type']

temp_msg ="无语"

    if u'Text' in msg_type:

temp_msg = msg['Text']

elif u'Recording' in msg_type:

temp_msg ='说的什么啊'

    elif u'Picture' in msg_type:

temp_msg ='这是黄图吗'

    else:

temp_msg ="这是什么啊"

    return get_response(temp_msg)

#用于接收群里面的对话消息#

@itchat.msg_register([itchat.content.TEXT,itchat.content.PICTURE,itchat.content.RECORDING,itchat.content.ATTACHMENT,itchat.content.VIDEO,itchat.content.MAP,itchat.content.VOICE,itchat.content.ATTACHMENT,itchat.content.NOTE,itchat.content.CARD], isGroupChat=True)

def print_content_group(msg):

msg_actualUserName = msg['ActualUserName']

msg_actual_nick_name = msg['ActualNickName']

from_user_name = msg['FromUserName']

toUserid = msg['ToUserName']

group = msg['User']

groupNickName = group['NickName']

groupId = group['UserName']

# if '@@f14e67cf03e051016ab2f7fb9e65c4cc7dbf070ab576782476c036fda89e3c11' in toUserid:#金家

    #    return '哦'

    # if '@@ffdfb0126f4b674e538c6fbd852e0f80f713801cab2dfd981645b7d2a84a05bd' in toUserid:#高层

    #    return '哦'

# if '@@3beb912db64a8faf7061e18a269ed8acdd0e08a4c975987044aea6c53f3d8f1e' in toUserid:

    #    return '哦'

    flag =0

    if '投资' not in groupNickName:

flag = flag +1

    if '屌丝' not in groupNickName:

flag = flag +1

    if '360' not in groupNickName:

flag = flag +1

    if flag ==3:

# 不可发送的群

        return None

    msg_id = msg['MsgId']

msg_from_user = msg['User']['NickName']

msg_content = msg['Content']

msg_create_time = msg['CreateTime']

msg_type = msg['Type']

temp_msg ="无语"

    if u'Text' in msg_type:

temp_msg = msg['Text']

elif u'Recording' in msg_type:

temp_msg ='说的什么啊'

    elif u'Picture' in msg_type:

temp_msg ='这是黄图吗'

    else:

temp_msg ="这是什么啊"

    return get_response(msg['Text'])

itchat.auto_login(True)

itchat.run()

相关文章

网友评论

      本文标题:微信聊天机器人

      本文链接:https://www.haomeiwen.com/subject/xgrxpqtx.html