好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python微信库:itchat的用法详解

本篇文章主要介绍了Python微信库:itchat的用法详解,现在分享给大家,也给大家做个参考。一起过来看看吧

import itchat
from itchat.content import TEXT

@itchat.msg_register
def simple_reply(msg):
  if msg['Type'] == TEXT:
    return 'I received: %s' % msg['Content']

itchat.auto_login()
itchat.run() 
#coding=utf8
import itchat
from itchat.content import *

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
  itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])

# 以下四类的消息的Text键下存放了用于下载消息内容的方法,传入文件地址即可
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
  msg['Text'](msg['FileName'])
  return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])

# 收到好友邀请自动添加好友
@itchat.msg_register(FRIENDS)
def add_friend(msg):
  itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
  itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])

# 在注册时增加isGroupChat=True将判定为群聊回复
@itchat.msg_register(TEXT, isGroupChat = True)
def groupchat_reply(msg):
  if msg['isAt']:
    itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])

itchat.auto_login(True)
itchat.run() 
import itchat
from itchat.content import TEXT

@itchat.msg_register(TEXT)
def simple_reply(msg):
  print(msg['Text'])

itchat.auto_login(hotReload=True)
itchat.run()
itchat.dump_login_status() 
import itchat
from itchat.content import TEXT

if itchat.load_login_status():
  @itchat.msg_register(TEXT)
  def simple_reply(msg):
    print(msg['Text'])
  itchat.run()
  itchat.dump_login_status()
else:
  itchat.auto_login()
  itchat.dump_login_status()
  print('Config stored, so exit.') 
# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
itchat.auto_login(enableCmdQR=2) 
import itchat, time, sys

def output_info(msg):
  print('[INFO] %s' % msg)

def open_QR():
  for get_count in range(10):
    output_info('Getting uuid')
    uuid = itchat.get_QRuuid()
    while uuid is None: uuid = itchat.get_QRuuid();time.sleep(1)
    output_info('Getting QR Code')
    if itchat.get_QR(uuid): break
    elif get_count >= 9:
      output_info('Failed to get QR Code, please restart the program')
      sys.exit()
  output_info('Please scan the QR Code')
  return uuid

uuid = open_QR()
waitForConfirm = False
while 1:
  status = itchat.check_login(uuid)
  if status == '200':
    break
  elif status == '201':
    if waitForConfirm:
      output_info('Please press confirm')
      waitForConfirm = True
  elif status == '408':
    output_info('Reloading QR Code')
    uuid = open_QR()
    waitForConfirm = False
userInfo = itchat.web_init()
itchat.show_mobile_login()
itchat.get_contract()
output_info('Login successfully as %s'%userInfo['NickName'])
itchat.start_receiving()

# Start auto-replying
@itchat.msg_register
def simple_reply(msg):
  if msg['Type'] == 'Text':
    return 'I received: %s' % msg['Content']
itchat.run() 
import itchat
from itchat.content import *

# 不带参数注册,所有消息类型都将调用该方法(包括群消息)
@itchat.msg_register
def simple_reply(msg):
  if msg['Type'] == 'Text':
    return 'I received: %s' % msg['Text']

# 带参数注册,该类消息类型将调用该方法
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
  itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName']) 
@itchat.msg_register(ATTACHMENT)
def download_files(msg):
  msg['Text'](msg['FileName']) 
import itchat
from itchat.content import TEXT

@itchat.msg_register(TEXT, isGroupChat = True)
def text_reply(msg):
  print(msg['isAt'])
  print(msg['ActualNickName'])
  print(msg['Content'])

itchat.auto_login()
itchat.run() 
import itchat
from itchat.content import *

itchat.auto_login()

@itchat.msg_register(TEXT)
def text_reply(msg):
  return 'This is the old register'

@itchat.msg_register(TEXT)
def text_reply(msg):
  return 'This is a new one'

itchat.run() 
import itchat
from itchat.content import *

itchat.auto_login()

@itchat.msg_register
def general_reply(msg):
  return 'I received a %s' % msg['Type']

@itchat.msg_register(TEXT)
def text_reply(msg):
  return 'You said to me one to one: %s' % msg['Text']

itchat.run() 
# 使用另一线程,但注意不要让程序运行终止
import thread

thread.start_new_thread(itchat.run, ())

# 使用configured_reply方法
while 1:
  itchat.configured_reply()
  # some other functions
  time.sleep(1) 
#coding=utf8
import thread

import itchat
from itchat.content import *

replyToGroupChat = True
functionStatus = False

def change_function():
  if replyToGroupChat != functionStatus:
    if replyToGroupChat:
      @itchat.msg_register(TEXT, isGroupChat = True)
      def group_text_reply(msg):
        if u'关闭' in msg['Text']:
          replyToGroupChat = False
          return u'已关闭'
        elif u'开启' in msg['Text']:
          return u'已经在运行'
        return u'输入"关闭"或者"开启"测试功能'
    else:
      @itchat.msg_register(TEXT, isGroupChat = True)
      def group_text_reply(msg):
        if u'开启' in msg['Text']:
          replyToGroupChat = True
          return u'重新开启成功'
    functionStatus = replyToGroupChat

thread.start_new_thread(itchat.run, ())

while 1:
  change_function()
  time.sleep(.1) 
#coding=utf8
import itchat

itchat.auto_login()
itchat.send('Hello world!')
# 请确保该程序目录下存在:gz.gif以及xlsx.xlsx
itchat.send('@img@%s' % 'gz.gif')
itchat.send('@fil@%s' % 'xlsx.xlsx')
itchat.send('@vid@%s' % 'demo.mp4') 
import itchat

itchat.auto_login()

itchat.send_msg('Hello world') 
#coding=utf8

import itchat

itchat.auto_login()

#请确保该程序目录下存在:xlsx.xlsx

itchat.send_file('xlsx.xlsx') 
#coding=utf8
import itchat

itchat.auto_login()
# 请确保该程序目录下存在:gz.gif
itchat.send_img('gz.gif') 
#coding=utf8

import itchat

itchat.auto_login()

#请确保该程序目录下存在:demo.mp4

itchat.send_file('demo.mp4') 
# 获取自己的用户信息,返回自己的属性字典
itchat.search_friends()
# 获取特定UserName的用户信息
itchat.search_friends(userName='@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.search_friends(name='littlecodersh')
# 获取分别对应相应键值的用户
itchat.search_friends(wechatAccount='littlecodersh')
# 三、四项功能可以一同使用
itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') 
# 获取特定UserName的公众号,返回值为一个字典
itchat.search_mps(userName='@abcdefg1234567')
# 获取名字中含有特定字符的公众号,返回值为一个字典的列表
itcaht.search_mps(name='LittleCoder')
# 以下方法相当于仅特定了UserName
itchat.search_mps(userName='@abcdefg1234567', name='LittleCoder') 
# 获取特定UserName的群聊,返回值为一个字典
itchat.search_chatrooms(userName='@abcdefg1234567')
# 获取名字中含有特定字符的群聊,返回值为一个字典的列表
itcaht.search_chatrooms(name='LittleCoder')
# 以下方法相当于仅特定了UserName
itchat.search_chatrooms(userName='@abcdefg1234567', name='LittleCoder') 
memberList = itchat.get_friends()[1:]
# 创建群聊,topic键值为群聊名
chatroomUserName = itchat.create_chatroom(memberList, 'test chatroom')
# 删除群聊内的用户
itchat.delete_member_from_chatroom(chatroomUserName, memberList[0])
# 增加用户进入群聊
itchat.add_member_into_chatroom(chatroomUserName, memberList[0]) 
def signin():
    # 查找公众号,进行签到
    user = itchat.search_mps(name='Nulll.me')
    UserName = user[0]['UserName']
    itchat.send(msg=u'3', toUserName=UserName)
    itchat.dump_login_status()
    pickleDumps('flag', localDay) # 如果执行成功写入标致文件
    exit()


if __name__ == '__main__':
    # 如果不是在登陆状态,就循环登陆
    while not itchat.load_login_status():
        sendMail()
        itchat.auto_login(hotReload=True)
        itchat.dump_login_status()
        signin() # 签到
        time.sleep(3600)
    signin() # 签到 

相关推荐:

python微信库itchat如何实现微信自动回复功能的代码实例

以上就是Python微信库:itchat的用法详解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python微信库:itchat的用法详解的详细内容...

  阅读:71次