同步memos到wordpress 的说说

2024-12-02 726 0

Puock主题自带了是光圈的功能,因此可以使用memos的webhook功能直接把说说同步到wordpress.

步骤

在主题文件functions.php中末尾处添加

function register_moments_post_type() {
    register_post_type('moments', 
        array(
            'public'       => true,
            'has_archive'  => true,
            'show_in_rest' => true,  // 确保这个参数设置为 true
            'supports'     => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
            'labels'       => array(
                'name'          => __('Moments'),
                'singular_name' => __('Moment'),
            ),
        )
    );
}
add_action('init', 'register_moments_post_type');

个人资料处获取应用程序密码

安装webhook
使用docker-compose部署

docker-compose.yaml的内容为

services:
  webhook:
    image: jkjoy/webhook
    container_name: webhook
    command: -verbose -hooks=hooks.yml -hotreload
    environment:
      - TZ=Asia/Chongqing #中国时区
      - LANG=C.UTF-8  #中文支持
    volumes:
      - ./config:/config
    ports:
      - 9000:9000
    restart: always

hooks.yml中添加

- id: wordpress
  execute-command: "/config/wordpress.sh"
  command-working-directory: "/"

wordpress.sh的内容为

#!/bin/bash

# 设置参数
MEMOS_URL="https://memos.ee/api/v1/memos?pageSize=1&filter=%20creator%20==%20%27users/1%27"
MEMOS_TOKEN=" " #后台获取的token
WP_SITE_URL="https://" #wordpress地址
WP_ENDPOINT="/wp-json/wp/v2/moments"
WP_USERNAME="admin" #wordpress的用户名
WP_APP_PASSWORD="0hc5 b9Ew 9pED ttZ8 8Qjn ms1Z" #后台获取到的应用密码
WP_AUTH="Authorization: Basic $(echo -n "${WP_USERNAME}:${WP_APP_PASSWORD}" | tr -d ' ' | base64)"

# Get Memos data
response=$(curl -s -H "Authorization: Bearer ${MEMOS_TOKEN}" -H "Accept: application/json" "${MEMOS_URL}")

if [ $? -eq 0 ] && echo "${response}" | jq -e . >/dev/null 2>&1; then
    echo "${response}" | jq -c '.memos[]' | while read -r memo; do
        memos_content=$(echo ${memo} | jq -r '.content')
        visibility=$(echo ${memo} | jq -r '.visibility')

        if [ "${visibility}" == "PUBLIC" ]; then
            # URL encode the content for search
            encoded_content=$(echo "${memos_content}" | jq -sRr @uri)
            already_posted=$(curl -s "${WP_SITE_URL}${WP_ENDPOINT}?search=${encoded_content}" | jq -r 'length // 0')

            if [[ "${already_posted}" =~ ^[0-9]+$ ]] && [ "${already_posted}" -eq 0 ]; then
                # Escape special characters in content
                escaped_content=$(echo "${memos_content}" | jq -R -s '.')

                # Create JSON payload with properly escaped content
                json_payload="{"title":"New Moment","content":${escaped_content},"status":"publish"}"

                # Send request with explicit content type
                curl -X POST 
                    "${WP_SITE_URL}${WP_ENDPOINT}" 
                    -H "${WP_AUTH}" 
                    -H "Content-Type: application/json; charset=UTF-8" 
                    -H "Accept: application/json" 
                    --data-binary "${json_payload}"
            fi
        fi
    done
fi

在memoswebhook处填入https://webhook/hooks/wordpress即可完成全部设置

相关文章

异物志
芒果网剧<我有一个朋友>
繁城之下
网剧《我叫赵甲第》
银河补习班 邓超的演技大爆发
长安十二时辰

发布评论