Skip to content

Instantly share code, notes, and snippets.

@fishyer
Created September 2, 2022 16:00
Show Gist options
  • Save fishyer/d9892d4b99e8423e27d4894a6c6556d5 to your computer and use it in GitHub Desktop.
Save fishyer/d9892d4b99e8423e27d4894a6c6556d5 to your computer and use it in GitHub Desktop.
一个简单的flask接口
from flask import Flask, jsonify, request, abort
import datetime
import requests
import json
import logging
app=Flask(__name__)
# 默认路由,内网穿透地址: http://fishyer-flask.vaiwan.com
@app.route('/')
def defaultRout():
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return 'start flask success: '+nowTime
@app.route('/hello')
def hello():
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sendMessageToFeishu("hello feishu")
return 'hello flask: '+nowTime
# 用于接受葫芦笔记的webhook,具体参数见:https://github.com/hulunote/documents/blob/master/R2D2WebHook%E4%BD%BF%E7%94%A8%E6%8E%A5%E5%8F%A3.md
@app.route('/webhook',methods=['POST'])
def webhook():
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(request.get_data())
return 'hello webhook: '+nowTime
@app.route('/receive',methods=['POST'])
def receive():
print('receive request')
requestData=request.get_data()
print(type(requestData))
requestJson=json.loads(requestData)
print(type(requestJson))
message=requestJson['message']
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"message: {message}")
return jsonify({'name': 'receive flask2',"time":nowTime,"message":message})
@app.route('/send',methods=['GET'])
def send():
print("-------------------[send]-------------------")
print(request.args)
message=request.args.get('message')
sendMessageToFeishu(message)
return f"send success: {message}"
def sendMessageToFeishu(message):
print(f"message:{message}")
url = "https://open.feishu.cn/open-apis/bot/v2/hook/b82250ce-23e0-4874-b539-da9370a923cf"
map={
"msg_type": "text",
"content": {
"text": message
}
}
payload = json.dumps(map)
headers = {
'Content-Type': 'application/json'
}
proxies = {
"http": None,
"https": None,
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False,proxies=proxies)
print(response.text)
if __name__=='__main__':
logging.warning('Watch out!')
app.run(host='0.0.0.0',debug=True,port='8800')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment