python itchat实现微信自动回复的示例代码

yipeiwu_com6年前Python基础

今天在实验楼发现一个特别好玩的,Python 微信库itchat,可以实现自动回复等多种功能,好玩到根本停不下来啊,尤其是调戏调戏不懂计算机的,特别有成就感,哈哈!!

代码如下:

#coding=utf8
import requests
import itchat

KEY = '8edce3ce905a4c1dbb965e6b35c3834d'

def get_response(msg):
  apiUrl = 'http://www.tuling123.com/openapi/api'
  data = {
    'key'  : KEY,
    'info'  : msg,
    'userid' : 'wechat-robot',
  }
  try:
    r = requests.post(apiUrl, data=data).json()
    return r.get('text')
  except:
    return

@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
  defaultReply = 'I received: ' + msg['Text']
  reply = get_response(msg['Text'])
  return reply or defaultReply

itchat.auto_login(hotReload=True)
itchat.run()

安装一下 itchat即可跑上面程序,实现与图灵机器人的交互。

更多关于itchat的资料,如下:

itchat官网 

Python微信库:itchat

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现隐马尔可夫模型的前向后向算法的示例代码

本篇文章对隐马尔可夫模型的前向和后向算法进行了Python实现,并且每种算法都给出了循环和递归两种方式的实现。 前向算法Python实现 循环方式 import numpy as...

Python使用sorted对字典的key或value排序

sorted函数 sorted(iterable,key,reverse) iterable 待排序的可迭代对象 key 对应的是个函数, 该函数用来决定选取用哪些值来进行排...

python bmp转换为jpg 并删除原图的方法

如下所示: # coding:utf-8 import os from PIL import Image # bmp 转换为jpg def bmpToJpg(file_path...

PHP魔术方法__ISSET、__UNSET使用实例

__isset()    – 在对类中属性或者非类中属性使用isset()方法的时候如果没有或者非公有属性,则自动执行__isset()的方法 __unset()  - 在对类...

安装python时MySQLdb报错的问题描述及解决方法

问题描述: windows安装python mysqldb时报错python version 2.7 required,which was not found in the regist...