基于Python的微信机器人实现

main代码如下

#coding=utf8
import sys
import os
from itchat.content import *
import itchat
from Plugin import *#自动加载插件
AI = False
AI_USE = []
def lc():
    print('登录成功')
def ec():
    print('帐号退出')
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True)
def text_reply(msg):
    deRevoke.savemsg(msg)
    global AI_USE
    if msg['Type'] == 'Text': 
        if msg['Text'] == '权限':
            if msg['FromUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa' or msg['ActualUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa':
                itchat.send('管理员权限',msg['FromUserName'])
            else:
                itchat.send('普通权限',msg['FromUserName'])
        elif msg['Text'] == '人工智障':
            global AI
            if msg['FromUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa' or msg['ActualUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa':
                AI = not AI
                itchat.send('状态为' + str(AI),msg['FromUserName'])
            else:
                itchat.send('无权更改',msg['FromUserName'])
        elif msg['Text'] == '人工智障加':
            if msg['FromUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa' or msg['ActualUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa':
                AI_USE.append(msg['FromUserName'])
                itchat.send('添加ok',msg['FromUserName'])
            else:
                itchat.send('无权更改',msg['FromUserName'])
        elif msg['Text'] == '人工智障删':
            if msg['FromUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa' or msg['ActualUserName'] == '@484d92cd4543eb528bac1fc9f6dbf7134f0136bd4a4b8d472c8fb853b969e4fa':
                AI_USE.remove(msg['FromUserName'])
                itchat.send('删除ok',msg['FromUserName'])
            else:
                itchat.send('无权更改',msg['FromUserName'])
        elif msg['Text'] == '一言':
            itchat.send(yiyan.yiyan(),msg['FromUserName'])
        elif msg['Text'] == '毒鸡汤':
            itchat.send(dujitang.dujitang(),msg['FromUserName'])
        elif msg['Text'] == '笑话':
            itchat.send(xiaohua.get_xiaohua(),msg['FromUserName'])
        elif msg['Text'] == '日图':
            itchat.send_image(bingritu.getritu(),msg['FromUserName'])
        elif "qq头像" in msg['Text']:
            itchat.send_image(qqtouxiang.qqtouxiang(regular(msg['Text'],"qq头像")),msg['FromUserName'])
        elif "二维码" in msg['Text']:
            itchat.send_image(getQrcode.getQrcode(regular(msg['Text'],"二维码")),msg['FromUserName'])
        elif "IP" in msg['Text'] or "ip" in msg['Text']:
            itchat.send(selectIp.selectIp(regular(regular(msg['Text'],"IP"),'ip')),msg['FromUserName'])
        elif "快递" in msg['Text']:
            itchat.send(kuaidi.kuaidi(regular(msg['Text'],"快递")),msg['FromUserName'])
        elif AI == True:
            if msg['FromUserName'] in AI_USE:
                itchat.send(tuling.get_tuling_respones(msg['Text']),msg['FromUserName'])
def regular(text,text2):
    return (text.replace(" ","")).replace(text2,"")
@itchat.msg_register([NOTE])
@itchat.msg_register([NOTE], isGroupChat=True)
def send_msg_helper(msg):
    deRevoke.replymsg(msg)
itchat.auto_login(hotReload=True,enableCmdQR=1,loginCallback=lc, exitCallback=ec)
itchat.run(True)

第六行会加载Plugin目录下所有py 以插件形式
下载地址
wechatbot.zip

Last modification:December 16th, 2018 at 06:31 am
If you think my article is useful to you, please feel free to appreciate

One comment

  1. hysteria

    微信要一直等录着很麻烦,后来就去玩Sever酱了

Leave a Comment