Mixin 机器人如何群发消息给用户

若需要给每个用户逐一发送消息(群发消息),就是发送消息到每个会话ID (Conversation ID)。而这份会话ID列表需要自己提前准备好,参考 Mixin 机器人如何获得用户ID和会话ID,然后自行存储和管理。

Mixin 的会话 ID 形式相同,但生成方法分两种:
1. 用户和用户之间的单人会话,可以通过计算两个用户的 user_id 获得。计算方法参考代码
2. 多人群组的会话ID是随机生成的。

Python 使用 nacl 生成密钥对

使用的库: PyNaCl
安装依赖: pip install PyNaCl

from nacl.signing import SigningKey
from nacl.public import PrivateKey


def generate_curve25519_keypair():
    # 生成私钥
    private_key = PrivateKey.generate()

    # 从私钥生成公钥
    public_key = private_key.public_key

    # 将私钥和公钥导出为字节形式,便于存储或传输
    private_key_bytes = private_key.encode()
    public_key_bytes = public_key.encode()

    # 打印密钥
    print(" ----- Curve25519 key pair -----")
    print("Private Key (hex):", private_key_bytes.hex())
    print("Public Key (hex):", public_key_bytes.hex())


# 适用于 JWT 签名
def generate_ed25519_keypair():
    # 生成私钥
    private_key = SigningKey.generate()

    # 从私钥生成公钥
    public_key = private_key.verify_key

    # 将私钥和公钥导出为字节形式,便于存储或传输
    private_key_bytes = private_key.encode()
    public_key_bytes = public_key.encode()

    # 打印密钥
    print(" ----- Ed25519 key pair -----")
    print("Private Key (hex):", private_key_bytes.hex())
    print("Public Key (hex):", public_key_bytes.hex())


if __name__ == "__main__":
    generate_curve25519_keypair()
    generate_ed25519_keypair()

Install pip3 on Ubuntu

本文章引用自: https://www.educative.io/edpresso/installing-pip3-in-ubuntu ,版权归原作者所有。

pip3 is the official package installer for Python 3. It can be used to install packages from the Python Package Index.

It is always a good idea to update before trying to install a new package. Run the command below:

sudo apt update

If Python 3 has already been installed on the system, execute the command below to install pip3:

Linux systemd unit 配置文件

基础概念可参考阮一峰写的 Systemd 定时器教程

[Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。

  • Type:定义启动时的进程行为。它有以下几种值。
  • Type=simple:默认值,执行ExecStart指定的命令,启动主进程
  • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
  • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
  • Type=dbus:当前服务通过D-Bus启动
  • Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
  • Type=idle:若有其他任务执行完毕,当前服务才会运行
  • ExecStart:启动当前服务的命令
  • ExecStartPre:启动当前服务之前执行的命令
  • ExecStartPost:启动当前服务之后执行的命令
  • ExecReload:重启当前服务时执行的命令
  • ExecStop:停止当前服务时执行的命令
  • ExecStopPost:停止当其服务之后执行的命令
  • RestartSec:自动重启当前服务间隔的秒数
  • Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-successon-failureon-abnormalon-aborton-watchdog
  • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
  • Environment:指定环境变量

Unit 配置文件的完整字段清单,请参考官方文档

Python之禅

如何显示?

import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

中文翻译:

JS 生成随机字符串

Generate random characters

Math.random().toString(36).replace(/[^a-z]+/g, '').slice(0, 5);

Math.random(),生成随机数,例如:0.09751664076957856
.toString(36),转换成36进制后的字符串。输出类似:“0.3idqid3cvvy”
.replace(/[^a-z]+/g, '') ,仅保留小写自负。输出类似:“idqidcvvy”
.slice(0, 5), 截取前5个字符。输出类似:"idqid"