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实现换位加密算法的示例

如下所示: def translationCipher(msg,key): result = [""]*key for i in range(key):#把每一列元素按...

Python玩转Excel的读写改实例

摘要: 利用xlrd读取excel 利用xlwt写excel 利用xlutils修改excel 利用xlrd读取excel 先需要在命令行中pip install xlr...

Python竟能画这么漂亮的花,帅呆了(代码分享)

Python竟能画这么漂亮的花,帅呆了(代码分享)

阅读本文大概需要3分钟 关于函数和模块讲了这么久,我一直想用一个好玩有趣的小例子来总结一下,同时也作为实战练习一下。 趣味编程其实是最好的学习途径,回想十几年前我刚毕业的时候,第一份工...

Python字符串转换成浮点数函数分享

利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456 from functools import reduce def s...

tensorflow中tf.slice和tf.gather切片函数的使用

tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连续区域的子集 tf.gather(params, indices, valida...