编写 webhook 接口用什么语言都可以,C/C++ 、Java、Python、node.js 等都可以。
我这里使用 Python 来写,由于 Ubuntu 一样平常自带 Python3 且 Python 语法简单。python3 --version if python3 is not installed, execute command sudo apt install python3 to install it.
Examine that you have installed pip and venv.
root@VM-0-17-ubuntu:/opt/webhooks/example# pip
Command 'pip' not found, but can be installed with:
(Windows 上激活虚拟环境,在 Command Prompt(CMD) 下使用 myenv\Scripts\activate.bat 激活。在 PowerShell 下激活,先设置 PowerShell 有实行脚本的权限:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser,然后实行 myenv\Scripts\Activate.ps1 激活,关闭 PowerShell 实行脚本权限 Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope CurrentUser 。答应 PowerShell 实行脚本通常不安全,不建议使用 PowerShell 激活 Python 虚拟环境。无论 Windows 照旧 Linux,取消虚拟环境的激活使用命令 deactivate 。)
激活了虚拟环境后,安装 Flask pip install Flask 。What’s Flask? Read Below:
(Flask is a popular, lightweight web framework for Python, used for building web applications and APIs. It’s classified as a micro-framework because it provides the essential tools and features for building web apps, but it doesn’t come with some of the heavier components that larger frameworks like Django provide (e.g., built-in ORM, form handling, etc.).
Flask is designed to be simple, flexible, and easy to extend, which makes it a great choice for both beginners and advanced developers. It allows you to build anything from small, simple websites to large, complex applications. Key Features of Flask:
Minimalistic and Lightweight: Flask doesn’t force you to use any specific tools or libraries. It’s designed to be flexible and allows you to use only what you need for your project.
Routing: Flask provides an easy-to-use routing system, which means you can map URLs to specific Python functions (known as views). For example, you can easily set up routes for different pages of your web application.
Template Engine: Flask uses the Jinja2 template engine, which allows you to separate the HTML structure from the Python code. It lets you embed dynamic content into HTML templates with the power of Python code.
Development Server and Debugger: Flask includes a built-in development server with an interactive debugger that makes it easy to test and debug your application.
RESTful Support: Flask is widely used for building REST APIs, thanks to its simplicity and the ability to integrate with tools like Flask-RESTful, Flask-SQLAlchemy, etc.
Extensibility: You can extend Flask with a wide variety of third-party libraries or plugins to add more features, such as database integration, authentication, and form validation.
Flexibility: Unlike Django, Flask gives you full control over how you structure your application. You can choose your own database, authentication methods, and other components.
WSGI Compliance: Flask is WSGI-compliant, meaning it’s compatible with any WSGI-compliant server, like Gunicorn or uWSGI, for production environments.) 编写 webhook 接口的 python 代码:
vim app.py: