微信公众号如何使用werobot进行开发

 

这几天突然想练习一下微信公众号的开发。于是去了一下,怎么使用。下面 是我的流程

一:注册

   这个我就不讲了,没意思,微信那里的流程写的很简单,一步步走就完事了

二:公众号连接服务器

  首先,你得有一个服务器, 对吧?

 然后,我查了一下,使用最方便的,还是python上的模块,werobot

下载方法。   pip install werobot

接着,就是连接服务器了。

https://mp.weixin.qq.com  进入开发者模块(页面最下面

这里你会有一个自己的token,方便服务器进行识别开发

举个例子,比如我的token是helloworld

代码就是:

import werobot

robot = werobot.WeRoBot(token='helloworld')

@robot.text
def hello_world():
    return 'Hello World!'

robot.run()

  你在服务器设置那里配置好后,微信会检测一次,成功后自动进行映射。你就可以和你的公众号聊天,发现只要是发文字,他就回复helloworld

那么,接下来是我这两天的研究。。werobot使用教程。(官方文档写的真的让我难受)

我先普及一下,

1 :@      在python是装饰器的用法,你可以理解为  新包装

2: weixin的消息包括很多种,我介绍几个简单的,

    一:text,也就是上面的 @robot.text  表示用户发送的是文本消息,你接下来的函数,就是对用户的文本消息,进行回复

    二:Imgae:表示用户发送的是图片,同理是  @robot.image

   此外,还包括了很多消息,比如点击了菜单栏,或者啥啥的

3:werobot模块,回复消息,是有这几种 :  

4 微信你如果要回复用户一张图片,那么是在参数里使用

reply = ImageReply(message=message,media_id=message.MediaId)

  是需要指定图片的ID的,而这种图片资源,一种是临时的,只保留3天,一种是永久素材,需要你去 上传

  上传有两种方法,一种是通过API 接口,一种是通过微信网站后台

5 有种玩意叫token。。。。微信开发,经常使用。。

 

然后就是喜闻乐见的贴代码环节了。

先贴一下我的服务器代码。最简单的版本,简单的回复文字消息

import werobot
import random
token=""  ###你的token
robot = werobot.WeRoBot(token=token)
# @robot.text 修饰的 Handler 只处理文本消息

@robot.text
def first(message, session):
	#if 'last' in session:
	#	return  "你已经有记录了"
	changdu=str(len(session))
	session[changdu] = message.content
	#print(type(session))#dict
	for k in session:
		print(session[k])
	return message.content
'''

@robot.text
def echo(message):
	print(type(message))#<class 'werobot.messages.messages.TextMessage'>
	print(type(message.content))#<class 'str'>
	return message.content
'''
# @robot.image 修饰的 Handler 只处理图片消息
@robot.image
def img(message):
	print(type(message))
	print(type(message.img))
	return message.img

robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()


然后是高级一点版本(改编某个博,地址我忘了抱歉 )

#coding:utf-8
from werobot import WeRoBot
import random
from werobot.replies import ArticlesReply, Article,ImageReply,TextReply
token='' ###你的token
robot = WeRoBot(token=token)

# 被关注
@robot.subscribe
def subscribe(message):
    return "欢迎关注我呀"
'''Hello World!
And nice to meet you.
:)
'''




# 从三首音乐里随机选一首
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]

# 读取 fight.txt 里的句子,随机返回一句
def get_fighttxt():
    filename = 'fight.txt'
    f = open(filename, 'r')
    data = f.read()
    f.close()
    data1 = data.split()
    max_num = len(data1) - 1
    num = random.randint(0, max_num)
    data2 = data1[num]
    return data2





# blog 回复个人博客
@robot.filter('blog')
def blog(message):
    reply = ArticlesReply(message=message)
    article = Article(
        title="v1coder",
        description="我的个人博客",
        img="https://werobot.readthedocs.io/zh_CN/latest/_static/qq.png",
        url="https://www.jianshu.com/u/7cb04d09491e"
    )
    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

# 匹配 音乐 回复一首歌
@robot.filter('test')
def music(message):
    reply = TextReply(message=message, content='Hello!')
    return reply

# 匹配 fight 回复一句话
@robot.filter('fight')
def fight(message):
    data = get_fighttxt()
    return data

# 文本消息返回原文
@robot.text
def echo(message):
    return message.content



robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()

 

然后是如何获取media_ID

 上传素材很简单,就在你的微信后台, 素材管理那里

如何查看呢?

上代码、

import requests
import json
ss=requests.session()
data2={
    "type": 'image',
    "offset": "0",
    "count": "2"
}
# type 表示种类
# offset表示起始位置
# count 表示返回数量
data=json.dumps(data2)  #数据需要json处理
print(data)
token="" #你的token
url='https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='+token
mm=ss.post(data=data,url=url)
print(mm.content)

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值