1. 程式人生 > >微信群助手(自動整理拼車資訊)

微信群助手(自動整理拼車資訊)

#!/usr/bin/env python
# coding: utf-8
import itchat, sys

reload(sys)
sys.setdefaultencoding('utf8')


def man(chatroomName):
    itchat.auto_login(hotReload=True)
    chatrooms = itchat.search_chatrooms(name=chatroomName)
    if chatrooms is None:
        print(u'沒有找到群聊:' + chatroomName)
    else:
        chatroom = itchat.update_chatroom(chatrooms[0]['UserName'])
        chatroom_user_name = chatroom.get("UserName")

    czr_dict = dict()
    rzc_dict = dict()

    @itchat.msg_register(itchat.content.TEXT, isGroupChat=True)
    def text_reply(msg):
        czr_str = u"**車找人: \n*釋出拼車資訊格式:車找人,18點一期到6區,缺2\n*取消行程格式:車找人,取消\n*檢視資訊:車找人,檢視"
        rzc_str = u"**人找車: \n*釋出拼車資訊格式:人找車,18點一期到6區,1人\n*取消行程格式:人找車,取消\n*檢視資訊:人找車,檢視"
        from_user = msg.get("FromUserName")
        to_user = msg.get("ToUserName")
        nike_name = msg.get("ActualNickName")

        def make_and_send(qstr, qdict):
            if u"取消" in text:
                if nike_name in qdict.keys():
                    qdict.pop(nike_name)
            elif u"清空" in text:
                qdict = dict()
            elif u"檢視" in text:
                pass
            else:
                qdict[nike_name] = text
            num = 1
            for k, v in qdict.items():
                if k and v:
                    qstr += u"\n" + str(num).decode("utf-8") + u". " + k + u"," + v
                    num += 1
            print qstr
            # if num != 1:
            itchat.send(qstr, toUserName=chatroom_user_name)

        if from_user == chatroom_user_name or to_user == chatroom_user_name:
            text = msg.get("Text")
            if text.startswith(u"車找人"):
                make_and_send(czr_str, czr_dict)
            if text.startswith(u"人找車"):
                make_and_send(rzc_str, rzc_dict)

    itchat.run()


if __name__ == '__main__':
    try:
        # chatroomName = u"2017款帕薩特"
        # chatroomName = u"527"
        chatroomName = u"沙河高教園↔上地拼車群(沙和上)"
        man(chatroomName)
    except Exception, e:
        print e