LoginSignup
0
0

zendeskの記事をコピペする

Last updated at Posted at 2023-10-03

やりたかったこと

zendeskのブランドAの記事をブランドBにコピーしたかった。
ブランドAの記事をブランドAの別セクションに保存するということもできると思う。

方法

zendeskがAPIを用意してくれているのでそれを使う。
スクリプトはrailsで書いた。

API を使用してヘルプセンターの記事のコピーを公開および同期する
https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/

手順

  1. コピー先のブランドの設定をする
  2. コピー先のセクションを作成する
  3. ベーシック認証を許可する
  4. スクリプトを実行する

1. コピー先のブランドの設定をする

ブランドがなければ作る。(※別ブランドにコピペする場合)

2. コピー先のセクションを作成する

コピー先のブランドに記事を作成するsectionを作成する。
今回はコピー先のsection数が少なかったので管理画面からポチポチ作成した。
ちゃんと調べていないがこれもスクリプトでできそう。
https://developer.zendesk.com/api-reference/help_center/help-center-api/sections/

3. ベーシック認証を許可する

通信時にベーシック認証を行うので設定からベーシック認証を許可する。

4. スクリプトを実行する

スクリプトの定数部分を必要な値に書き換える。
スクリプトをrails consoleとかで実行する。

class CopyZendesk
  require "net/http"
  # 以下を設定
  LOGIN = "" # ベーシック認証のアドレス
  PASS = ""  # ベーシック認証のパスワード
  BASE_DOMEIN = "" # コピー元のサブドメイン
  NEW_DOMEIN = ""  # コピー先のサブドメイン
  # コピー元、コピー先のsection_idのマッピング
  SECTIONS = [
    {section_id: '', new_section_id: ''},
    {section_id: '', new_section_id: ''},
    {section_id: '', new_section_id: ''},
    {section_id: '', new_section_id: ''},
    {section_id: '', new_section_id: ''},
    {section_id: '', new_section_id: ''}
  ]

  def copy
    SECTIONS.each do |section|
      articles = get_section_articles(section[:section_id])
      articles.reverse!
      articles.each do |article|
        post_article(article: article, section_id: section[:new_section_id])
        sleep 0.4
      end
    end
  end

  private

  def get_section_articles(section_id)
    uri = URI("https://#{BASE_DOMEIN}.zendesk.com/api/v2/help_center/ja/sections/#{section_id}/articles")
    request = Net::HTTP::Get.new(uri, "Content-Type": "application/json")
    request.basic_auth LOGIN, PASS
    response = Net::HTTP.start uri.hostname, uri.port, use_ssl: true do |http|
      http.request(request)
    end

    unless response.code.to_i == 200
      raise "[取得失敗]{code:#{response.code}"
    end

    JSON.parse(response.body)["articles"]
  end

  def post_article(article:, section_id:)
    uri = URI("https://#{NEW_DOMEIN}.zendesk.com/api/v2/help_center/sections/#{section_id}/articles")
    request = Net::HTTP::Post.new(uri, "Content-Type": "application/json")
    params = {
      "article": {
        "title": article["title"],
        "body": article["body"],
        "locale": article["locale"],
        "user_segment_id": article["user_segment_id"],
        "permission_group_id": article["permission_group_id"],
        "draft": article["draft"] # いきなり公開したくない場合はtrueを指定
      },
      "notify_subscribers": false # trueにすると作成するたびにメールが飛ぶので一括作成の場合はfalseにした方がよい
    }.to_json
    request.body = params
    request.basic_auth LOGIN, PASS
    response = Net::HTTP.start uri.hostname, uri.port, use_ssl: true do |http|
      http.request(request)
    end

    unless response.code.to_i == 201
      raise "[作成失敗]{code: #{response.code}"
    end
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0