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设计】。

相关文章

Django自定义用户登录认证示例代码

Django自定义用户登录认证示例代码

前言 有时候 Django 自带的用户登录认证不能满足我们的需求,比如我不想要用户名+密码登录,我想手机号+验证码登录,这样就需要我们去修改 Django 自带的认证了。 Django...

基于python进行桶排序与基数排序的总结

本文首先举例阐述了两种排序方法的操作步骤,然后列出了用python进行的实现过程,最后对桶式排序方法的优劣进行了简单总结。 一、桶排序: 排序一个数组[5,3,6,1,2,7,5,10]...

详细介绍Python函数中的默认参数

import datetime as dt def log_time(message, time=None): if time is None: time=dt.da...

python提示No module named images的解决方法

本文讲述了python提示No module named images的解决方法,非常实用!分享给大家供大家参考。具体方法如下: 出现提示:ImportError: No module...

python设置值及NaN值处理方法

如下所示: python 设置值 import pandas as pd import numpy as np dates = pd.date_range('20180101',pe...