本篇文章開啟Python開發(fā)微信公眾號(hào)后臺(tái)
準(zhǔn)備:
云服務(wù)平臺(tái):(我用的是京東云的)
公眾號(hào):自己注冊(cè)一個(gè)就行
Git:代碼管理平臺(tái)
了解平臺(tái)
進(jìn)入公眾號(hào)后臺(tái)暖释,查看開發(fā)相關(guān)項(xiàng)
基本配置(服務(wù)器配置等):
開發(fā)者工具(包括開發(fā)文檔哀军,測試工具等):
基本功能code
Python數(shù)據(jù)分析實(shí)戰(zhàn)
開發(fā)使用的是平臺(tái)提供的werobot框架
# -*- coding: utf-8 -*-
# @Time : 2019/12/17 7:57 PM
# @Author : Python數(shù)據(jù)分析實(shí)戰(zhàn)
# @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')
# 訂閱后的回復(fù)
@robot.subscribe
def subscribe():
return "***歡迎關(guān)注公眾號(hào)[愉快][愉快][愉快]***\n" \
"***輸入任意內(nèi)容開始與我聊天!\n" \
"***輸入'博客'關(guān)注我的博客!\n" \
"***輸入'音樂'為小主送上舒緩的歌曲!\n"
# 關(guān)鍵字 博客 回復(fù)
@robot.filter('博客')
def blog(message):
reply = ArticlesReply(message=message)
article = Article(
title="Python數(shù)據(jù)分析實(shí)戰(zhàn)",
description="我的個(gè)人博客",
img="https://werobot.readthedocs.io/zh_CN/latest/_static/qq.png",
url="http://www.reibang.com/u/bdf11cce83a1"
)
reply.add_article(article)
return reply
# 用戶發(fā)送圖片
@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
# 隨機(jī)一首音樂
def music_data():
music_list = [
['童話鎮(zhèn)','陳一發(fā)兒','https://e.coka.la/wlae62.mp3','https://e.coka.la/wlae62.mp3'],
['都選C','縫紉機(jī)樂隊(duì)','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]
# 匹配 音樂 回復(fù)一首歌
@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
# 文字智能回復(fù)
@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 + ' 公眾號(hào)(機(jī)器人)' + ':' + response)
# return response
return "該功能有待處理哮洽,敬請(qǐng)期待"
# 讓服務(wù)器監(jiān)聽在 0.0.0.0:80
robot.config['HOST']='0.0.0.0'
robot.config['PORT']=80
robot.run()
搭建服務(wù)
可將代碼上傳至服務(wù)器填渠,然后運(yùn)行即可
上傳至服務(wù)器方式:
一、通過GIT 上傳鸟辅,然后服務(wù)器下載
二氛什、通過本地上傳至服務(wù)器
配置參數(shù)
參數(shù)說明:
開發(fā)者ID與開發(fā)者密碼:一對(duì)秘鑰,后面會(huì)用到匪凉,可以保存下來屉更,也可通過重置獲取
IP白名單:添加自己的服務(wù)器IP
服務(wù)器地址:即服務(wù)器的IP
令牌:自定義字符串 與 代碼中的 Token保持一致
所有配置項(xiàng)配置完成。
那么恭喜你洒缀,已走出后臺(tái)開發(fā)的第一步瑰谜。
歡迎關(guān)注,后面會(huì)持續(xù)更新
公眾號(hào)可聯(lián)系作者