One, foreword
In our daily work, study and life, we may encounter the following situations:
- A server managed by oneself breaks down, but is not alerted in time, resulting in service loss
- Some websites that they really want to register quietly open registration, but they did not know in time, so they can only continue to wait aimlessly
- …
If we spend time paying attention to everything, we inevitably run out of time, so is there a way to get these messages together and push them in a timely manner? Here I would like to recommend a solution, that is to use Serverless + Flying book to create their own personalized message reminder system.
Two, preparation work
-
First sign up for an account, and then log in to the flybook web page
-
Open feishu open platform, click create enterprise self-built application, and enter the application name and application subtitle, and then click OK to create
- Click the newly created application in the enterprise self-built application list and record the App ID and App Secret
Write code
-
Create a local project directory (feishu-notify is used as an example)
-
Create three files:.env, index.py, and serverless.yml
-
Code as described below
.env
TENCENT_SECRET_ID=AKID********************************
TENCENT_SECRET_KEY=********************************
Copy the code
Note: TENCENT_SECRET_ID and TENCENT_SECRET_KEY here can be obtained from the access key of Tencent Cloud Console. If there is no key, you need to create one yourself
serverless.yml
myFunction:
component: "@serverless/tencent-scf"
inputs:
name: feishu-notify-py
codeUri: ". /"
handler: index.main_handler
runtime: Python36.
region: ap-guangzhou
description: My Serverless Function Used to Notify Myself
memorySize: 128
events:
- apigw:
name: serverless
parameters:
protocols:
- https
endpoints:
- path: "/"
method: POST
Copy the code
Note: You can click here to see a property list of all available properties in serverless.yml
index.py
def main_handler(event, context): import requests import json print(event) CONFIG = { "app_id": "********************", "app_secret": "********************************" } # my auth if 'myauth' not in event['queryString'] or event['queryString']['myauth'] ! = 'feishu1': return 'forbidden' # Get content postContent = event['body'] try: postContent = json.loads(postContent) except: return 'error in json_loads(line: 19)' if 'content' not in postContent: return 'invalid params' content = postContent['content'] # Get tenant_access_token try: res = requests.post('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/', { "app_id": CONFIG['app_id'], "app_secret": CONFIG['app_secret'] }) except: return 'error in get_tenant_access_token' data = json.loads(res.text) if data['code'] ! = 0: return data['msg'] token = data['tenant_access_token'] # Get chat_id try: res = requests.get('https://open.feishu.cn/open-apis/chat/v4/list', headers={ 'Authorization': 'Bearer %s' % (token) }) except: return 'error in get_chat_id' data = json.loads(res.text) if data['code'] ! = 0: return data['msg'] groupList = data['data']['groups'] myGroupId = groupList[0]['chat_id'] # Send message try: res = requests.post('https://open.feishu.cn/open-apis/message/v4/send/', json={ "chat_id": myGroupId, "msg_type": "text", "content": { "text": content } }, headers={ 'Authorization': 'Bearer %s' % (token), 'Content-Type': 'application/json' }) except: return 'error in send message' data = json.loads(res.text) if data['code'] ! = 0: return data['msg'] return 'success'Copy the code
There are a few points to make about index.py:
- In the code
app_id
和app_secret
Items to be filled in in the preparation of work recordsApp ID 和 App Secret - Ultimately we use
POST
Method to send a message - When we call it, we also need to call it
query
In add? myauth=feishu1
For simple verification to prevent others from sending, for examplehttps://service-********-**********.**.apigw.tencentcs.com/release/?myauth=feishu1
3. Deploy the Serverless service
- Install Serverless using NPM
$ npm install -g serverless
Copy the code
- through
serverless
Command to deploy and add--debug
Parameter View information during the deployment
$ serverless --debug
Copy the code
- The URL of the API gateway is obtained from the terminal
Gets the URL of the API gateway
Iv. Online application
-
Back on the Flypu open platform, click on the newly created application in the list of enterprise self-built applications
-
Click Apply Function – Robot, and click Enable Robot
- Click Version management and Release – Create Version, and refer to the following figure for configuration (do not click save for now).
- In the availability state, click Edit, select all employees, and click Save
- Click to apply for publication
- Click on the profile picture of flying book web version to enter the background of flying book management
- Go to Workbench – Apply Audit, then audit
- Click through
Call interface
Request mode: POST
Request address: The URL of the APIGateway obtained above
The request Header:
parameter | type | Required/optional | instructions | The default value | The instance |
---|---|---|---|---|---|
Content-Type | string | mandatory | Content-Type | application/json |
Request Query:
parameter | type | Required/optional | instructions | The default value | The instance |
---|---|---|---|---|---|
myauth | string | mandatory | Simple authentication | feishu1 |
The request Body:
{
"content": "Fill in the information that you want to send."
}
Copy the code
Six, effects,
For convenience, this is done using the Chrome plugin Talend API Tester
As you can see, the fly-book notifications are very timely
Seven, conclusion
In fact, the use of flying books can achieve not only these, I believe that smart you will be able to develop more interesting applications, this share to the end.
One More Thing
What can you do in three seconds? Take a sip of water, read an email, or — deploy an entire Serverless application?
Copy the link to PC browser to access: serverless.cloud.tencent.com/deploy/expr…
3 seconds fast deployment, immediately experience the history of the fastest Serverless HTTP actual combat development!
Portal:
- GitHub: github.com/serverless
- Website: serverless.com
Welcome to: Serverless Chinese, you can experience more about Serverless application development in best practices!
Recommended reading: Serverless Architecture: From Principle, Design to Project Practice