阿里云学生服务器搭建微信订阅号后台

  最近阿里云学生服务器推出了学生服务器https://developer.aliyun.com/adc/student/,免费6个月。领取后打算先搭建一个微信订阅号后台。

在freenom.com/zh/index.html上注册一个域名bigicebox.tk 。详细方法是https://www.bilibili.com/video/BV1RE411P7i2?from=search&seid=16008602093501017388,不挂vpn,地址填当前ip的地址(我虽然不在成都,但是https://ip8.com/查询在,那我就该填写成都)域名的设置https://zhuanlan.zhihu.com/p/115535965,域名解析http://www.mamicode.com/info-detail-2854414.html。阿里云配置https://www.jianshu.com/p/cf227b34b1e5

微信服务号开发https://www.jianshu.com/p/f04867dcef0d

 

使用WinSCP远程连接阿里云轻量应用服务器https://blog.csdn.net/jinxlzc/article/details/89459682

pip3安装https://blog.csdn.net/LONG_Yi_1994/article/details/86224357

阿里云开放80端口https://yq.aliyun.com/articles/701181

 

Ubuntu查看和释放80端口https://blog.csdn.net/licheetools/article/details/82946328

经历了2天终于完成了功能。

#coding:utf-8
# Filename:hello_world.py
# 验证服务器,并且收到的所有消息都回复'Hello World!'

import werobot
from werobot.replies import ArticlesReply, Article
import requests
from werobot import WeRoBot
import re
import json
import random

tgurl = "https://v1.alapi.cn/api/dog"   #舔狗api
twurl = "https://v1.alapi.cn/api/qinghua" #土味情话
jturl = "https://v1.alapi.cn/api/soul"    #鸡汤
payload = "format=text"
headers = {'Content-Type': "application/x-www-form-urlencoded"}


musicid = "id=732264691"
token = "yourtoken" #你自定义的
robot = werobot.WeRoBot(token=token)



@robot.subscribe
def subscribe(message):
    return '''Hello World!
And nice to meet you.
:)请输入“help”或者“帮助”查看互动方法
'''
def help_data():
    filename = 'help.txt'
    f = open(filename, 'r')
    data = f.read()
    f.close()
    return data


@robot.filter(re.compile(".*?help.*?"), "帮助")
def help(message):
    data = help_data()
    return data

def love_data():
    url = "https://chp.shadiao.app/api.php"
    res = requests.get(url)
    data = res.text
    return data

def quick_data():
    url = "https://v1.hitokoto.cn/"
    r = requests.get(url)
    re = r.text
    res = json.loads(re)
    data1 = res["hitokoto"]
    data2 = res["from"]
    data = data2 +":" +data1
    return data

def music_data():
    url = "https://v1.alapi.cn/api/music/playlist"
    payload1 = musicid
    response = requests.request("POST", url, data=payload1, headers=headers)
    re = response.text
    res = json.loads(re)
    num = random.randint(0,9)
    data1 = res["data"]["playlist"][num]["name"]
    data2 = res["data"]["playlist"][num]["user_name"]
    data3 = res["data"]["playlist"][num]["url"]
    music = [data1,data2,data3] 
    return music

def photo_data(message):
    url = "https://v1.alapi.cn/api/acg"
    payload2 = "format=json"
    headers = {'Content-Type': "application/x-www-form-urlencoded"}
    response = requests.request("POST", url, data=payload2, headers=headers)
    re = response.text
    res = json.loads(re)
    urrl = res["data"]["url"]
    reply = ArticlesReply(message=message)
    article = Article(
        title = "随机ACG",
        description = "这是一张随机的ACG图片,希望你喜欢",
        img = urrl,
        url = urrl
    )
    reply.add_article(article)
    return reply

@robot.filter("photo", "图片")
def photo(message):
    photo1 = photo_data(message)
    return photo1


# 匹配 音乐 回复一首歌
@robot.filter("music", "音乐")
def music(message):
    music1 = music_data()
    return music1

@robot.filter(re.compile(".*?love.*?"), "彩虹屁")
def love(message):
    pi = love_data()
    return pi

# blog 回复个人博客
@robot.filter(re.compile(".*?blog.*?"), "博客")
def blog(message):
    reply = ArticlesReply(message=message)
    article = Article(
        title="BIGICEBOX",
        description="我的个人博客",
        img="https://s1.ax1x.com/2020/07/02/NqGCbd.jpg",
        url="https://www.cnblogs.com/bigicebox/"
    )
    reply.add_article(article)
    return reply

@robot.filter("舔狗","lovedog")
def td(message):
    dog1 = tg_data()
    return dog1
    
def tg_data():
    response = requests.request("POST", tgurl, data=payload, headers=headers)
    dog=response.text
    return dog

@robot.filter(re.compile(".*?土味.*?"),re.compile(".*?情话.*?"),"duststory")
def qinghua(message):
    qinghua1 = qinghua2()
    return qinghua1

def qinghua2():
    response = requests.request("POST", url=twurl, data=payload, headers=headers)
    q=response.text
    return q
    
@robot.filter("鸡汤","soup")
def jitang(message):
    jitang1 = jitang2()
    return jitang1

def jitang2():
    response = requests.request("POST", url=jturl, data=payload, headers=headers)
    j=response.text
    return j
# @robot.handler 处理所有消息
@robot.handler
def hello(message):
    data = quick_data()
    return data


# 让服务器监听在 0.0.0.0:80
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()

 

posted @ 2020-07-02 23:07  bigicebox  阅读(436)  评论(4编辑  收藏  举报