zoukankan      html  css  js  c++  java
  • Python微信公众号后台开发教程<001>

    ​本篇文章开启Python开发微信公众号后台

    准备:

    云服务平台:(我用的是京东云的)

    公众号:自己注册一个就行

    Git:代码管理平台

    了解平台

    进入公众号后台,查看开发相关项

    基本配置(服务器配置等):

    开发者工具(包括开发文档,测试工具等):


    基本功能code

    开发使用的是平台提供的werobot框架

    # -*- coding: utf-8 -*-
    # @Time    : 2019/12/17 7:57 PM
    # @Author  : Python数据分析实战
    # @File    : main.py
    # @Software: PyCharm
    
    import random
    import time
    import requests
    import werobot
    from werobot.replies import ArticlesReply, Article, ImageReply, TextReply, MusicReply
    
    robot=werobot.WeRoBot(token='自己的token')
    
    
    # 订阅后的回复
    @robot.subscribe
    def subscribe():
        return "***欢迎关注公众号[愉快][愉快][愉快]***
    " 
               "***输入任意内容开始与我聊天!
    " 
               "***输入'博客'关注我的博客!
    " 
               "***输入'音乐'为小主送上舒缓的歌曲!
    "
    
    
    # 关键字 博客 回复
    @robot.filter('博客')
    def blog(message):
        reply = ArticlesReply(message=message)
        article = Article(
            title="Python数据分析实战",
            description="我的个人博客",
            img="https://werobot.readthedocs.io/zh_CN/latest/_static/qq.png",
            url="https://www.jianshu.com/u/bdf11cce83a1"
        )
        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("
    "+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
    
    
    # 文字智能回复
    @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
    
        return "该功能有待处理,敬请期待"
    
    
    # 让服务器监听在 0.0.0.0:80
    robot.config['HOST']='0.0.0.0'
    robot.config['PORT']=80
    robot.run()
    

    搭建服务

    Python数据分析实战

    可将代码上传至服务器,然后运行即可

    上传至服务器方式:

                一、通过GIT 上传,然后服务器下载

                二、通过本地上传至服务器


    配置参数

    Python数据分析实战

    参数说明

    开发者ID与开发者密码:一对秘钥,后面会用到,可以保存下来,也可通过重置获取

    IP白名单:添加自己的服务器IP

    服务器地址:即服务器的IP

    令牌:自定义字符串  与 代码中的 Token保持一致

    所有配置项配置完成。

    那么恭喜你,已走出后台开发的第一步。

    欢迎关注,后面会持续更新。

     公众号可加入微信交流群

  • 相关阅读:
    Node自动重启工具 nodemon
    centos 集群
    kettle操作数据库增删改
    Dinic
    vim
    mermaid简介
    联赛模拟测试32
    检讨书模板
    博客园如何添加看板娘!
    手机浏览器如何调试
  • 原文地址:https://www.cnblogs.com/blogs/p/12119968.html
Copyright © 2011-2022 走看看