python post方法登录微信公众号_python实现简单的微信公众号后台编写
利用python的werobot库实现对微信公众号后台的开发:
实现的简单功能:
1.订阅后的回复
2.机器人聊天回复
3.指定关键字回复音乐,文字,链接
3.图片返回原图
注意点:
1.该后台基于微信官方提供的接口实现,需要先完成微信公众服务平台的注册和相关配置。
2.后台代码架设的服务器ip信息需要填写到微信公众服务平台,且端口只能是指定的80(https)或者443(http).
代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
import time
import requests
import werobot
from werobot.replies import ArticlesReply, Article, ImageReply, TextReply, MusicReply
robot=werobot.WeRoBot(token='')
# 订阅后的回复
@robot.subscribe
def subscribe():
return "***欢迎关注公众号[愉快][愉快][愉快]***\n" \
"***输入任意内容开始与我聊天!\n" \
"***输入'博客'关注我的博客!\n" \
"***输入'音乐'为小主送上舒缓的歌曲!\n"
# 关键字 博客 回复
@robot.filter('博客')
def blog(message):
reply = ArticlesReply(message=message)
article = Article(
title="忧郁的炸酱面",
description="我的个人博客",
img="https://werobot.readthedocs.io/zh_CN/latest/_static/qq.png",
url="https://www.jianshu.com/u/3c58aa6164de"
)
reply.add_article(article)
return reply
# 用户发送图片
@robot.image
def blog(message,session):
#print("msg", message.img)
#print(type(message))
#print(type(message.img))
#print(message.__dict__)
print("\n"+message.MediaId)
changdu = str(len(session))
session[changdu] = message.MediaId
reply = ImageReply(message=message, media_id=message.MediaId)
return reply
# 随机一首音乐
def music_data():
music_list = [
['童话镇','陈一发儿','https://e.coka.la/wlae62.mp3','https://e.coka.la/wlae62.mp3'],
['都选C','缝纫机乐队','https://files.catbox.moe/duefwe.mp3','https://files.catbox.moe/duefwe.mp3'],
['精彩才刚刚开始','易烊千玺','https://e.coka.la/PdqQMY.mp3','https://e.coka.la/PdqQMY.mp3']
]
num = random.randint(0,2)
return music_list[num]
# 匹配 音乐 回复一首歌
@robot.filter('音乐')
def music(message):
# reply = TextReply(message=message, content=music_data())
# reply = MusicReply(message=message,source='https://www.kugou.com/song/#hash=D4EB517A405FCDF0286AA9A4487BBCE1&album_id=10409377')
return music_data()
# return reply
# 调用智能回复接口
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key': '43826fe7f1e74add804dd47cf2791228', # Tuling Key,API的值
'info': msg, # 发出去的消息
'userid': '441670', # 用户名
}
r = requests.post(apiUrl, data=data).json() # post请求
return r.get('text')
# 文字智能回复
@robot.text
def replay(msg):
print(msg.content)
curtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
response = get_response(msg.content)
print(
curtime + ' 公众号(机器人)' + ':' + response)
return response
# 让服务器监听在 0.0.0.0:80
robot.config['HOST']='0.0.0.0'
robot.config['PORT']=80
robot.run()#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
import time
import requests
import werobot
from werobot.replies import ArticlesReply, Article, ImageReply, TextReply, MusicReply
robot=werobot.WeRoBot(token='liuang123456')
# 订阅后的回复
@robot.subscribe
def subscribe():
return "***欢迎关注公众号[愉快][愉快][愉快]***\n" \
"***输入任意内容开始与我聊天!\n" \
"***输入'博客'关注我的博客!\n" \
"***输入'音乐'为小主送上舒缓的歌曲!\n"
# 关键字 博客 回复
@robot.filter('博客')
def blog(message):
reply = ArticlesReply(message=message)
article = Article(
title="忧郁的炸酱面",
description="我的个人博客",
img="https://werobot.readthedocs.io/zh_CN/latest/_static/qq.png",
url="https://www.jianshu.com/u/3c58aa6164de"
)
reply.add_article(article)
return reply
# 用户发送图片
@robot.image
def blog(message,session):
#print("msg", message.img)
#print(type(message))
#print(type(message.img))
#print(message.__dict__)
print("\n"+message.MediaId)
changdu = str(len(session))
session[changdu] = message.MediaId
reply = ImageReply(message=message, media_id=message.MediaId)
return reply
# 随机一首音乐
def music_data():
music_list = [
['童话镇','陈一发儿','https://e.coka.la/wlae62.mp3','https://e.coka.la/wlae62.mp3'],
['都选C','缝纫机乐队','https://files.catbox.moe/duefwe.mp3','https://files.catbox.moe/duefwe.mp3'],
['精彩才刚刚开始','易烊千玺','https://e.coka.la/PdqQMY.mp3','https://e.coka.la/PdqQMY.mp3']
]
num = random.randint(0,2)
return music_list[num]
# 匹配 音乐 回复一首歌
@robot.filter('音乐')
def music(message):
# reply = TextReply(message=message, content=music_data())
# reply = MusicReply(message=message,source='https://www.kugou.com/song/#hash=D4EB517A405FCDF0286AA9A4487BBCE1&album_id=10409377')
return music_data()
# return reply
# 调用智能回复接口
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key': '43826fe7f1e74add804dd47cf2791228', # Tuling Key,API的值
'info': msg, # 发出去的消息
'userid': '441670', # 用户名
}
r = requests.post(apiUrl, data=data).json() # post请求
return r.get('text')
# 文字智能回复
@robot.text
def replay(msg):
print(msg.content)
curtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
response = get_response(msg.content)
print(
curtime + ' 公众号(机器人)' + ':' + response)
return response
# 让服务器监听在 0.0.0.0:80
robot.config['HOST']='0.0.0.0'
robot.config['PORT']=80
robot.run()
微信展示的功能如下:
图片发自简书App
微信图片_20190519141247.jpg
python post方法登录微信公众号_python实现简单的微信公众号后台编写相关推荐
- python通过微信聊天记录分析_Python利用itchat对微信中好友数据实现简单分析的方法...
前言 最近在一个微信公众号上看到一个调用微信 API 可以对微信好友进行简单数据分析的一个包 itchat 感觉挺好用的,就简单尝试了一下. 库文档说明链接在这: itchat 安装 在终端中输入以下 ...
- python wechatpay微信支付回调_python服务器 实现app微信支付:支付异步通知
# -*- coding: utf-8 -*- import tornado.httpserver import tornado.ioloop import tornado.options impor ...
- python爬虫搜特定内容的论文_python爬取指定微信公众号文章
python怎么抓取微信阅清晨的阳光比不上你的一缕微笑那么动人,傍晚的彩霞比不上你的一声叹息那么心疼,你的一个个举动,一句句话语都给小编带来无尽的幸福. 抓取微信公众号的文章 一.思路分析 目前所知晓 ...
- python爬虫爬取公众号_Python selenium爬取微信公众号文章代码详解
需求: 想阅读微信公众号历史文章,但是每次找回看得地方不方便. 思路: 1.使用selenium打开微信公众号历史文章,并滚动刷新到最底部,获取到所有历史文章urls. 2.对urls进行遍历访问,并 ...
- python爬取新浪新闻首页_Python爬虫学习:微信、知乎、新浪等主流网站的模拟登陆爬取方法...
微信.知乎.新浪等主流网站的模拟登陆爬取方法 摘要:微信.知乎.新浪等主流网站的模拟登陆爬取方法. 网络上有形形色色的网站,不同类型的网站爬虫策略不同,难易程度也不一样.从是否需要登陆这方面来说,一些 ...
- python post 请求登录后的页面_python - 学习模拟登录,卡在发送POST请求之后?
问 题 学习模拟登录,发送了post请求之后页面还是登录页面啊,不知道原因 import re import requests url='http://cer.imufe.edu.cn/authser ...
- python爬取微信好友信息_python itchat 爬取微信好友信息
「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚空 三张图分别是「微信好友头像拼接图」.「性别统计图」.「个性签名统计图」 「微信好友头像拼接图」 「性别统计 ...
- python微信投票代码_Python——开发一个自动化微信投票器【附代码实例方法】
一个研究Python实践,最近研究一个投票的东东,主要是想测试利用Python实现刷微信投票. 本文纯粹为了记录一下 webdriver直接操作页面按钮的方法: #!/usr/bin/python # ...
- python实现微信自动发信息_Python实现智慧-定期向微信女友发送消息,python,智给,定时,发消息...
#author_='zhi'; #date: 2020/6/20 0020 13:34 from __future__ import unicode_literals from threading i ...
最新文章
- “趣冷淡”怎么看?网络电话寻求共同语言
- Mybatis简单的入门之增删改查
- C#中窗口关闭时没有取消事件订阅导致事件重复执行的解决方法
- 通过rpm包安装、配置及卸载mysql的详细过程.
- mysql sql 去除重复行_mysql – sql自连接表删除重复行
- lapis 框架安装试用
- 【原创】Extjs4 通用CURD方法
- 【C语言】学习gotoxy() 与 clrscr() 函数
- 通达信标记符号_通达信在股票名称后自动标记
- SPSS数据分析常见问题(差异性研究)
- 《编译学习计划【第一季】》- C语言中的qsort函数
- Mac item2 配色,大小写敏感及常用快捷键
- Unity的UGUI使用Text和Image实现文字下划线
- 保送清华计算机,高二学霸保送清华,怎么做到的?
- GitLab CI/CD .gitlab-ci.yaml 关键词(三):制品artifacts,缓存cache
- 选取一段代码块一下子都回退或前进空一格的快捷键
- 通过jmeter进行用户并发(vu/s)测试
- CRMEB Pro v1.2快速打包 APP步骤详解
- react的props效验规则
- Android elevation设置大揭秘