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

  1. First sign up for an account, and then log in to the flybook web page

  2. Open feishu open platform, click create enterprise self-built application, and enter the application name and application subtitle, and then click OK to create

  1. Click the newly created application in the enterprise self-built application list and record the App ID and App Secret

Write code

  1. Create a local project directory (feishu-notify is used as an example)

  2. Create three files:.env, index.py, and serverless.yml

  3. 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:

  1. In the codeapp_idapp_secretItems to be filled in in the preparation of work recordsApp IDApp Secret
  2. Ultimately we usePOSTMethod to send a message
  3. When we call it, we also need to call itqueryIn add? myauth=feishu1For simple verification to prevent others from sending, for examplehttps://service-********-**********.**.apigw.tencentcs.com/release/?myauth=feishu1

3. Deploy the Serverless service

  1. Install Serverless using NPM
$ npm install -g serverless
Copy the code
  1. throughserverlessCommand to deploy and add--debugParameter View information during the deployment
$ serverless --debug
Copy the code
  1. The URL of the API gateway is obtained from the terminal

Gets the URL of the API gateway

Iv. Online application

  1. Back on the Flypu open platform, click on the newly created application in the list of enterprise self-built applications

  2. Click Apply Function – Robot, and click Enable Robot

  1. Click Version management and Release – Create Version, and refer to the following figure for configuration (do not click save for now).

  1. In the availability state, click Edit, select all employees, and click Save

  1. Click to apply for publication

  1. Click on the profile picture of flying book web version to enter the background of flying book management

  1. Go to Workbench – Apply Audit, then audit

  1. 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