学习python的微信公众号_python学习—实现微信公众号聊天机器人
最近在学习python,想做一个聊天机器人,百度了很多资料,有的也是根据别人分享的文档一步步操作,但过程中还是遇到了一些问题,因此 我自己总结了一下我的步骤:
1. 申请一个公众号, 具体的可以百度微信公众号跟着步骤申请一个就行 自己玩的话 订阅号就可以了
2. 申请一个服务器
我用的是新浪云,不要问我为什么选择这个, 我也是刚开始跟着一个教程做的
创建一个云应用
image
选择python开发语言 这里我选择的共享环境 具体计费及配额旁边都有说明,输入二级域名(自己起个名字就行),输入应用名称—->确认创建
新浪云的代码管理有两种方式 SVN 和git 为了方便我选择了git
3. 在本地建一个文件夹 weixin
新建config.yaml文件name: wxpytest
version: 1
libraries:
- name: webpy
version: "0.36"
- name: lxml
version: "2.3.4"
新建index.wsgi文件# coding: UTF-8
import os
import sae
import web
from weixinInterface import WeixinInterface
urls = (
'/weixin','WeixinInterface'
)
app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, 'templates')
render = web.template.render(templates_root)
app=web.application(urls, globals()).wsgifunc()
application = sae.create_wsgi_app(app)
新建weixinInterface.py文件# -*- coding: utf-8 -*-
import hashlib
import web
import lxml
import time
import os
from lxml import etree
import urllib2
import robot
class WeixinInterface:
def __init__(self):
self.app_root = os.path.dirname(__file__)
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
def GET(self):
#获取输入参数
data = web.input()
signature=data.signature
timestamp=data.timestamp
nonce=data.nonce
echostr = data.echostr
#自己的token
token="你的token" #这里改写你在微信公众平台里输入的token
#字典序排序
list=[token,timestamp,nonce]
list.sort()
sha1=hashlib.sha1()
map(sha1.update,list)
hashcode=sha1.hexdigest()
#sha1加密算法
#如果是来自微信的请求,则回复echostr
if hashcode == signature:
return echostr
新建一个文件夹/weixin/templates,新建一个文件reply_text.xml$def with (toUser,fromUser,createTime,content)
$createTime
4.设置微信公众号,进入开发—>基本配置
image.png
例如我的域名是test.applinzi.com
ur填写为:https://test.applinzi.com/weixin
查找自己的域名参考下图:
image.png
token和代码里写的token保持一致就行
EncodingAesKey 随机生成就好了
消息加密方式选择明文或兼容都可以
点击提交
如果出现token验证失败请检查你的token和代码里填写的是否一致,url是否正确
5. 调用图灵api实现机器人聊天
首先需要去图灵官网注册 开通一个机器人
新建robot.py# -*- coding=utf-8 -*-
import requests
KEY = "创建机器人后生成的apikey"
def get_response(msg):
apiUrl='http://www.tuling123.com/openapi/api'
data={
'key':KEY,
'info':msg,
'userid':'机器人名字',
}
try:
r=requests.post(apiUrl, data=data).json()
return r.get('text').encode("utf-8")
except:
return msg
return msg
修改weixinInterface.py文件# -*- coding: utf-8 -*-
import hashlib
import web
import lxml
import time
import os
from lxml import etree
import urllib2
import robot
class WeixinInterface:
def __init__(self):
self.app_root = os.path.dirname(__file__)
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
def GET(self):
#获取输入参数
data = web.input()
signature=data.signature
timestamp=data.timestamp
nonce=data.nonce
echostr = data.echostr
#自己的token
token="********" #这里改写你在微信公众平台里输入的token
#字典序排序
list=[token,timestamp,nonce]
list.sort()
sha1=hashlib.sha1()
map(sha1.update,list)
hashcode=sha1.hexdigest()
#sha1加密算法
#如果是来自微信的请求,则回复echostr
if hashcode == signature:
return echostr
def POST(self):
str_xml = web.data()#获得post来的数据
xml = etree.fromstring(str_xml)#进行XML解析
msgType=xml.find("MsgType").text
fromUser=xml.find("FromUserName").text
toUser=xml.find("ToUserName").text
if msgType == 'text':
content=xml.find("Content").text
rpyMsg=robot.get_response(content)
return self.render.reply_text(fromUser,toUser,int(time.time()), rpyMsg)
else:
pass
由于robot导入了requests包,我们需要把requests,urllib3,idna,chardet,certifi包拉到本地放在weixin目录下
并且修改index.wsgi文件# coding: UTF-8
import os
import sae
import web
import sys
from weixinInterface import WeixinInterface
urls = (
'/weixin','WeixinInterface'
)
app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, 'templates')
render = web.template.render(templates_root)
app=web.application(urls, globals()).wsgifunc()
application = sae.create_wsgi_app(app)
sys.path.insert(0,os.path.join(app_root, 'requests'))
sys.path.insert(0,os.path.join(app_root, 'urllib3'))
sys.path.insert(0,os.path.join(app_root, 'chardet'))
sys.path.insert(0,os.path.join(app_root, 'certifi'))
sys.path.insert(0,os.path.join(app_root, 'idna'))
学习python的微信公众号_python学习—实现微信公众号聊天机器人相关推荐
- 圆方圆学院零基础入门学习Python(绝对干货,值得学习)
圆方圆学院零基础入门学习Python(绝对干货,值得学习) 链接: pan.baidu.com/s/1Shpd1G8L- 提取码: bup7
- 小甲鱼零基础入门学习Python(绝对干货,值得学习)
小甲鱼零基础入门学习Python(绝对干货,值得学习) 链接: https://pan.baidu.com/s/1jJmIrlk 密码: ktp2
- python操作微信电脑版_Python学习教程:教你用Python通过微信来控制电脑摄像头
如果说强大的标准库奠定了Python发展的基石,丰富的第三方库则是python不断发展的保证.今天的Python学习教程就来通过itchart库来实现通过微信对电脑的一些操作. 1.安装库 安装itc ...
- python训练营微信广告发送机_python实现给微信公众号发送消息的方法
本文实例讲述了python实现给微信公众号发送消息的方法.分享给大家供大家参考,具体如下: 现在通过发微信公众号信息来做消息通知和告警已经很普遍了.最常见的就是运维通过zabbix调用shell脚本给 ...
- python自动推送消息_Python自动接收微信群消息并推送相应的公众号文章
原标题:Python自动接收微信群消息并推送相应的公众号文章 封面图片:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社 用书教师可以联系 ...
- python微信点赞脚本_Python爬取微信公众号评论、点赞等相关信息
微信公众号爬虫方案分析(爬取文艺相处公众号) 之前考虑过使用搜狗微信来爬取微信公众号信息,不过搜狗提供的数据有诸多弊端,比如文章链接是临时的,文章没有阅读量等指标,所以考虑通过手机客户端利用 Pyth ...
- python爬取微信公众号_Python爬取微信公众号(中间人代理法)
1.环境:ubuntu16.04 + redis + mysql + python3.5 + anyproxy + android + pm2 + npm + node 一台爬虫服,python3环境 ...
- python微信集成_Python微信公众号后台开发005:集成智能聊天机器人
给公众号集成一个智能聊天机器人 一.前述 ChatterBot是一个基于机器学习的聊天机器人引擎,构建在python上,主要特点是可以自可以从已有的对话中进行学(jiyi)习(pipei). 二.具 ...
- python 画在同一坐标轴_Python学习第95课-多个数据在同一个坐标轴画图叠加
[每天几分钟,从零入门python编程的世界!] 我们已经学过了绘制折线图.柱状图.直方图.散点图.饼状图和堆栈图. 这节课我们学习如何把多个数据画图叠加,这种图形可以用作多组数据的对比. 下面我们通 ...
最新文章
- squid 优化指南
- 收藏 | CNN的一些可视化方法!
- c++与java中子类中调用父类成员的方法
- 日志处理--高效Linux命令整理
- 支持linux系统摄像头模块,在Linux操作系统上使用摄像头
- 做一个常规的banner图——负边距的使用、banner图的拼法
- Easyspy网络检测系统
- 2013年4月管理计算机应用,全国2013年4月高等教育自学考试管理系统中计算机应用试题及答案...
- 编程体系结构(04):JavaIO流文件管理
- Kindle的对手来了?华为首款鸿蒙墨水平板国行发布时间曝光...
- 【译】Effective TensorFlow Chapter11——在TensorFlow中调试模型
- 熊猫to_csv()–将DataFrame转换为CSV
- 给服务器端DropDownList控件添加客户端onchange事件
- cmos逻辑门传输延迟时间_终于有人能把常用的三种电平:TTL、CMOS、RS232电平讲明白了...
- 智能自动感应手消毒器方案开发
- mysql is marked_快速解决MySQL:Table xxx is marked as crashed and should be repaired五个办法...
- php怎么获得今天的日期,PHP怎么获取今天、昨天、明天的日期-php教程
- 【unmatched host】nfs挂载 提示“: refused mount request from xx for /nfsdata (/nfsdata): unmatched host ”
- 随机过程4-宽平稳过程,严平稳过程的定义和判定
- 自定义php模板引擎
热门文章
- 监听器(Listener)
- 在你所有的项目中都加入 TailwindCSS 吧!
- 计算机网络安全ipc默认共享,IPC$、ADMIN$、C$、D$都是什么?如何关闭取消删除Windows默认共享...
- linux装入归档文件时,装入归档文件时发生了错误!!怎么解决?
- spring-自动装配
- 计算机图形学浙江大学第一章图形学概述
- 为什么越学反而越蠢?碎片化学习是个骗局
- Reaction Paper
- Clipper库中文文档(ClipperLib)
- 力扣OJ(1401-1500)