Pthon——第三方模块(itchat)

博客介绍了微信助手的相关操作,包括给文件助手发消息,执行发送内容后,根据执行情况显示结果。还提到了统计微信男女好友及其他比例,以及创建机器人等内容。
  1. 给微信助手发消息:

    import itchat
    import time
    import random
    
    itchat.auto_login()
    
    while True:
        itchat.send('hello',toUserName='filehelper')
        time.sleep(random.randint(1,3))   ##随机隔1~3秒
    
  2. 给文件助手发送消息,执行发送的内容
    1).如果执行成功,显示执行结果
    2).如果执行失败,显示执行失败

    @itchat.msg_register(itchat.content.TEXT)
    def text_reply(msg):
        if msg['ToUserName'] == 'filehelper':
            #获取要执行命令的内容
            command = msg['Content']
            print(command)
            #让电脑执行命令代码
            #如果执行成功,返回值为0
            if os.system(command) == 0:
                res = os.popen(command).read()
                result = '【返回值】 - 命令执行成功,执行结果:\n' + res
                itchat.send(result,'filehelper')
            #命令执行失败,请重新输入命令
            else:
                result = '【返回值】 - 命令%s执行失败,请重试' %command
                itchat.send(result,'filehelper')
    
    itchat.auto_login()
    itchat.run()
    
    1. 统计微信男女好友以及其他比例:

        import itchat
       import time
       import random
       itchat.auto_login()
       friends = itchat.get_friends()
       info = {}
       for friend in friends[1:]:
           if friend['Sex'] == 1:
               info['male'] = info.get('male',0) +1
           elif friend['Sex'] == 2:
               info['female'] =info.get('female',0) +1
           else:
               info['other'] = info.get('other',0) +1
       
       print(info)
      
  3. 创建机器人:

    import itchat
    import requests
    
    def get_tuling_reponse(_info):
        print(_info)
        api_url = 'http://www.tuling123.com/openapi/api'
        data = {
            'key':'28a1d488a7fe47b5b637b750a6f3d66b',
            'info':_info,
            'userid':'haha'
        }
        # 发送数据到指定的网址,获取网址返回的数据
        res = requests.post(api_url,data).json()
        #print(res,type(res))
        # 给用户返回的内容
        print(res['text'])
        return (res['text'])
    
    # get_tuling_reponse('给我讲个笑话')
    # get_tuling_reponse('不好笑')
    
    # 时刻监控好友发送的文本信息,并且给与一个回复
    @itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
    def text_repky(msg):
        # 获取好友发送的文本信息
        # 返回文本信息
        content = msg['Content']
        # 将好友的消息发送给机器人去处理,处理的结果就是返回给好友的信息
        returnContent = get_tuling_reponse(content)
        return returnContent
    
    itchat.auto_login()
    itchat.run()
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值