Python itchat.search_friends() Examples

The following are 11 code examples of itchat.search_friends(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module itchat , or try the search function .
Example #1
Source File: chat_utils.py    From spider_python with Apache License 2.0 6 votes vote down vote up
def send_to_person(username, file_names):
    """
    发送给某个人
    :param username: 发送对象的昵称
    :param filename: 文件名
    :return:
    """
    room = itchat.search_friends(name=r'%s' % username)

    userName = room[0]['UserName']

    try:
        if isinstance(file_names, list):
            # 多个图片
            for file_name in file_names:
                itchat.send_image(file_name, toUserName=userName)
        else:
            # 一个图片
            itchat.send_image(file_names, toUserName=userName)
        print('发送完毕!')
    except:
        print('发送出错!') 
Example #2
Source File: chat_utils.py    From tools_python with Apache License 2.0 5 votes vote down vote up
def send_word_to_person(name, content):
    """
    发送消息给某个人
    :param name:
    :param content:
    :return:
    """
    users = itchat.search_friends(name)
    userName = users[0]['UserName']
    itchat.send(content, toUserName=userName) 
Example #3
Source File: chat_utils.py    From tools_python with Apache License 2.0 5 votes vote down vote up
def send_file_to_person(name, filename):
    """
    发送文件给某个人
    :param name:
    :param filename:
    :return:
    """
    users = itchat.search_friends(name)
    userName = users[0]['UserName']
    itchat.send_file(filename, toUserName=userName) 
Example #4
Source File: WechatAddGroupHelper.py    From WechatAddGroupHelper with MIT License 5 votes vote down vote up
def is_online():
    """
    判断微信是否在线
    :return: Bool
    """
    try:
        if itchat.search_friends():
            return True
    except IndexError:
        return False
    return True 
Example #5
Source File: WeChatAssistant.py    From WeChatAssistant with Apache License 2.0 5 votes vote down vote up
def SendFriend(self):
        global frind_dict
        for i in range(len(frind_dict)):
            if(self.d_Listname.selection_includes(i) == True):
                key = self.d_Listname.get(i)
                value = frind_dict[key]
                str = self.d_input.get(1.0, END)
                self.d_input.delete(1.0, END)
                frind = itchat.search_friends(nickName=value)[0]['UserName']
                itchat.send(str,toUserName=frind) 
Example #6
Source File: wechat.py    From anack with GNU General Public License v3.0 5 votes vote down vote up
def SendText2Friend(msg,nick_name='filehelper'): #已经测试成功,可用
    '''
    @ 发送文本消息给指定好友,如果不指定nick_name则发送给自己的文件助手
    '''
    if nick_name == 'filehelper':
        itchat.send(msg,toUserName = 'filehelper')
    else:
        
        users = itchat.search_friends(name=nick_name)
#        print(users)
        who = users[0]['UserName']
#        print(who)
        itchat.send(msg,toUserName = who) 
Example #7
Source File: group_helper.py    From EverydayWechat with MIT License 5 votes vote down vote up
def get_city_by_uuid(uid):
    """
    通过用户的uid得到用户的城市
    最好是与机器人是好友关系
    """
    itchat.get_friends(update=True)
    info = itchat.search_friends(userName=uid)
    # print('info:'+str(info))
    if not info:
        return None
    city = info.city
    # print('city:'+city)
    return city 
Example #8
Source File: itchat_helper.py    From EverydayWechat with MIT License 5 votes vote down vote up
def get_friend(wechat_name, update=False):
    """
    根据用户名获取用户数据
    :param wechat_name: str 用户名
    :param update: bool 强制更新用户数据
    :return: obj 单个好友信息
    """
    if update: itchat.get_friends(update=True)
    if not wechat_name: return None
    friends = itchat.search_friends(name=wechat_name)
    if not friends: return None
    return friends[0] 
Example #9
Source File: main.py    From weixinqunzhushou with Apache License 2.0 5 votes vote down vote up
def add_friend(msg):
    itchat.add_friend(**msg['Text'])  # 该操作会自动将新好友的消息录入,不需要重载通讯录
    user_info = itchat.search_friends(userName=msg['RecommendInfo']['UserName'])
    itchat.send_msg(u'Hi,我是一个智能机器人,能帮助您自动化的管理微信群,把我拉入群,我就可以开始为你工作啦', user_info['UserName'])
    upsert_user(user_info) 
Example #10
Source File: 25 PythonItChat.py    From Python-Spider with Apache License 2.0 4 votes vote down vote up
def text_reply(msg):
    print msg
        # 微信里,每个用户和群聊,都使用很长的ID来区分
        if msg["MsgType"] == 49:
            print "个人分享文章地址链接Url:" + "---------------------------"
                
                xmlcontent = lxml.etree.HTML(get_html(msg["Url"]))
                print xmlcontent
                    title = xmlcontent.xpath('//h2[@class="rich_media_title"]/text()')

                    imgArray = xmlcontent.xpath('//img[@data-type="png"]/@data-src')
                        # 下载图片
                        source = xmlcontent.xpath('//span[@class="rich_media_meta rich_media_meta_text rich_media_meta_nickname"]/text()')
                        time = xmlcontent.xpath('//em[@class="rich_media_meta rich_media_meta_text"]/text()')
                        print "来源"
                            print source, time
                                # 下载图片
                                print "下载图片"
                                    # print imgArray
                                    # print title[0]
                                    get_image(title, imgArray, source, time,msg["Url"])

                                    print msg["Url"]
                                        print "个人分享文章类型编号MsgType:" + "---------------------------"
                                            print msg["MsgType"]
                                            print "个人分享Content:" + "---------------------------"
                                                print msg["Content"]
                                                print "个人分享FromUserName:" + "---------------------------"
                                                    print msg["FromUserName"]
                                                    print "个人分享ToUserName:" + "---------------------------"
                                                        print msg["ToUserName"]
                                                        print "个人分享链接标题FileName:" + "---------------------------"
                                                            print msg["FileName"]
                                                        
                                                            print "------------个人"
                                                                # 获取到的信息是某某人和登录者之间的通讯,如果不是和登录这通讯就获取不到
                                                                print itchat.search_friends(userName=msg['FromUserName'])['NickName']
                                                                print itchat.search_friends(userName=msg['ToUserName'])['NickName']
                                                                    
                                                                    else:
                                                                        print "不是个人分享的文章"


# 处理群聊消息 
Example #11
Source File: main.py    From EverydayWechat with MIT License 4 votes vote down vote up
def is_online(auto_login=False):
    """
    判断是否还在线。
    :param auto_login: bool,当为 Ture 则自动重连(默认为 False)。
    :return: bool,当返回为 True 时,在线;False 已断开连接。
    """

    def _online():
        """
        通过获取好友信息,判断用户是否还在线。
        :return: bool,当返回为 True 时,在线;False 已断开连接。
        """
        try:
            if itchat.search_friends():
                return True
        except IndexError:
            return False
        return True

    if _online(): return True  # 如果在线,则直接返回 True
    if not auto_login:  # 不自动登录,则直接返回 False
        print('微信已离线..')
        return False

    # hotReload = not config.get('is_forced_switch', False)  # 切换微信号,重新扫码。
    hotReload = False  # 2019年9月27日15:31:22 最近保存最近登录状态出错,所以先设置每次都得扫码登录
    loginCallback = init_data
    exitCallback = exit_msg
    try:
        for _ in range(2):  # 尝试登录 2 次。
            if platform.system() in ('Windows', 'Darwin'):
                itchat.auto_login(hotReload=hotReload,
                                  loginCallback=loginCallback, exitCallback=exitCallback)
                itchat.run(blockThread=True)
            else:
                # 命令行显示登录二维码。
                itchat.auto_login(enableCmdQR=2, hotReload=hotReload, loginCallback=loginCallback,
                                  exitCallback=exitCallback)
                itchat.run(blockThread=True)
            if _online():
                print('登录成功')
                return True
    except Exception as exception:  # 登录失败的错误处理。
        sex = str(exception)
        if sex == "'User'":
            print('此微信号不能登录网页版微信,不能运行此项目。没有任何其它解决办法!可以换个号再试试。')
        else:
            print(sex)

    delete_cache()  # 清理缓存数据
    print('登录失败。')
    return False