0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LINEのLIFFv2とOAをほぼ素人が一日でつくってみた!

Last updated at Posted at 2020-01-11

LINEのLIFFとOA(オフィシャルアカウント)をどう作ってみたか

2019年にLIFFのv2がリリースされ、作ってみたいと思っていたが、やっとやろうと思いました!
そして、作成していたらOAと連動する流れがあり、おまけでOAも作りました!

ぶっちゃけ感想

Webアプリを作ってリリース経験が無い私が挑みましたが、本当に簡単だし、これからちゃんとしたものを作ろうと思いました!

作成説明の流れ

  1. 実際に公開済みのWebアプリを用意
  2. Developers にログイン
  3. プロバイダーを作成
  4. 色々設定
  5. WebアプリをLIFF用に色々変更
  6. LIFF完成!
  7. OAの作成!
  8. OAのアドミン設定

LIFFとOAの説明

LIFF とは

LINE Front-end Framework(LIFF)は、LINEが提供するウェブアプリのプラットフォームです。このプラットフォームで動作するウェブアプリを、LIFFアプリと呼びます。

OA とは

LINE公式アカウントのこと

1. 実際に公開済みのWebアプリを用意

githubで後悔したWebアプリにします。

  • 新しいリポジトリを作成
  • index.htmlを作成(下のコードは参考までに)
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>HAPPY NEW YEAR</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <h1>HAPPY NEW YEAR</h1>
    <h2>LIFFtest's Page!</h2>
  </body>
  <script>
  </script>
</html>
  • settingでGitHub Pagesをmaster branchにする
setting.png スクリーンショット 2020-01-10 18.18.54.png
  • するとURLが発行される!
スクリーンショット 2020-01-10 18.19.02.png
  • 公開されているか確認!
スクリーンショット 2020-01-10 18.21.57.png
  • 公開されてました!
    これでとりあえずLINEのアカウントの方に移動します!

2. Developers にログイン

スクリーンショット 2020-01-10 17.53.19.png
  • 「LINEアカウントからログイン」を選択
スクリーンショット 2020-01-10 17.43.47.png

3. プロバイダーを作成

  • プロバイダーを作成
スクリーンショット 2020-01-10 17.53.38.png
  • 「作成」ボタンを選択し、名前を入れ、下の作成ボタンを押す
スクリーンショット 2020-01-10 17.54.15.png
  • ファイルが作成されたらチャネルを選択(今回は左のLINEログインを選択)
スクリーンショット 2020-01-10 17.54.png
  • 必要な内容を入力し、作成ボタンを押す(メールはフィクションです。自分のメールをちゃんと設定してください)
    make_provider.png

  • プロバイダー作成完了!

4. LIFFを作成する

  • でも、まだLIFFではありません!
スクリーンショット 2020-01-10 21.59.16.png
  • 「追加」を選択し設定をする(エンドポイントURLはgithubで最初に作成したURLを貼る)
    LIFF_setting.png

  • LIFFのURL作成された!!!

スクリーンショット 2020-01-10 22.21.10.png

これをLINEのどこかのトークに送りそれをタップ!
できています!!
IMG_5381.PNG

5. WebアプリをLIFF用に色々変更

LIFFで色々できるらしいですが、LoginとgetProfileとsendMessageとcloseWindowをやってみます!
こちらのAPIの説明をもとに作成した。

0. SDKを設定

<script src="https://static.line-scdn.net/liff/edge/2.1/sdk.js"></script>

1. LIFF init(LIFF初期化) 2. Login(ウェブアプリ向けのLINEログインの処理)

liffIdはLIFF用に作成されたURLの最後の部分である

// <script>タブ内の話
liff.init({liffId}).then(() => {
 if (liff.isLoggedIn()) {
 }
 else{
  liff.login();
 }
})

3. getProfile(LOGINしてる人のLINE情報を取得)

ボタンを作成し、それが押されたら以下のfunctionが呼ばれるようにした。
以下では、ログインしてるユーザーの名前とプロフィール画像とステータスメッセージを取得する。
最後に、ボタンあたりのコードを載せるのでそちらで確認してください。

// <script>タブ内の話
function showProfile() {
      liff.getProfile()
      .then(profile => {
        const name = profile.displayName;
        const pic = profile.pictureUrl;
        const status = profile.statusMessage;
        console.log('name', name);
        console.log('pic', pic);
        console.log('status', status);
        Name.innerHTML = name;
        Status.innerHTML = status;
        Img.src = pic;
      })
      .catch((err) => {
        console.log('error', err);
      });
    }

4. sendMessage(トークに自動でメッセージを送信)

LIFFからトークにメッセージを送信することができる。 
LIFF上にあるボタンを押したらメッセージが送られるように作成した。
以下は、タイプを指定し、送信する内容を指定すればOK.

// <script>タブ内の話
const text = "I sent test message!"
    function sendMessage() {
      liff.sendMessages(
        [{ type: "text", text}]
        ).then(function() {
            window.alert('Message sent');
        }).catch(function(error) {
            window.alert('Error sending message: ' + error);
        });
    }

5. closeWindow(LIFFを閉じる)

こちらも、ボタンを押したらLIFFが閉じられる仕様にした。

// <script>タブ内の話
function closeWindow() {
      if (!liff.isInClient()) {
          sendAlertIfNotInClient();
      } else {
          liff.closeWindow();
      }
    }

すべてを含めたコード

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>MINAKO TEST LIFF</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style>
      .btn {
        height: 40px;
        border: #DDD;
        box-shadow: 0 4px 4px #BBB;
      }
      .flex {
        margin-bottom: 10px;
        width: 100%;
        text-align:center;
      }
      .img {
        border-radius: 50%;
        width: 100px;
        height: 100px;
      }
      .h1 {
        text-align:center; 
        width: 100%;
      }
      .h2 {
        text-align:center;
        width: 100%;
      }
    </style>
    <!-- Load LIFF SDK -->
    <script src="https://static.line-scdn.net/liff/edge/2.1/sdk.js"></script>
  </head>
  <body>
    <div class="flex">
      <h1>MINAKO TEST LIFF</h1>
      <h2>Welcome!</h2>
    </div>
    <div class="flex">
      <button class="btn" id="showProfile">showProfile</button>
      <button class="btn" id="sendMessage">sendMessage</button>
    </div>
    <div class="flex">
      <img id="profile_img" class="img" src=""/>
      <p id="profile_name"></p>
      <p id="profile_status"></p>
    </div>
    <div class="flex">
      <button class="btn" id="closeWindowButton">close</button>
    </div>
  </body>
  <script>
    // My LIFF ID
    const liffId = "1653746756-2Lrl3yld"
    // Text I want to send
    const text = "I sent test message!"
    // Containers displaying profile information
    const Name = document.getElementById('profile_name');
    const Img = document.getElementById("profile_img");
    const Status = document.getElementById("profile_status");

    // Callbacks being called after clicking the button
    function showProfile() {
      liff.getProfile()
      .then(profile => {
        const name = profile.displayName;
        const pic = profile.pictureUrl;
        const status = profile.statusMessage;
        console.log('name', name);
        console.log('pic', pic);
        console.log('status', status);
        Name.innerHTML = name;
        Status.innerHTML = status;
        Img.src = pic;
      })
      .catch((err) => {
        console.log('error', err);
      });
    }

    function sendMessage() {
      liff.sendMessages(
        [{ type: "text", text}]
        ).then(function() {
            window.alert('Message sent');
        }).catch(function(error) {
            window.alert('Error sending message: ' + error);
        });
    }

    function closeWindow() {
      if (!liff.isInClient()) {
          sendAlertIfNotInClient();
      } else {
          liff.closeWindow();
      }
    }

    liff.init({liffId}).then(() => {
      if (liff.isLoggedIn()) {
      }
      else{
        liff.login();
      }
    })

    // Declaration to bind buttons and callbacks
    document.querySelector("#showProfile").addEventListener("click", showProfile)
    document.querySelector("#sendMessage").addEventListener("click", sendMessage)
    document.getElementById('closeWindowButton').addEventListener('click', closeWindow)

  </script>
</html>

6. LIFF完成!

「5.WebアプリをLIFF用に色々変更」をおわらせ、LINEであるトークにそのLIFFのURLを貼り、それを自分の端末で開く!
もし、思うように動かなかったら自分のコードを確認し、また、LIFFをiPhoneで開くを繰り返しデバックをする。
ちなみに、

ezgif-1-1ee9014e00a6.gif

以上のようになった。

7. OAの作成

OAを作ろうと思ったきっかけはこちら

OA作成方法の詳細はこちら

OAと合体させると、LIFFを初めて開いた時の認可画面でOAの友だち追加を促すことが可能。
連携がユーザー側にも伝わり、とても便利です。

  1. ここの右の「未認証アカウントを解説する」を選択
スクリーンショット 2020-01-11 22.56.33.png
  1. いくつか設定
スクリーンショット 2020-01-11 22.57.18.png

ついにOA作成完了!
スクリーンショット 2020-01-11 22.57.31.png

8. OAのアドミン設定

  1. ↑の画像の「LINEOfficial Account Managerへ」を選択

  2. 右上の「設定」→「Messaging API」でMessagingAPIの設定

スクリーンショット 2020-01-11 23.04.11.png
  1. 利用するボタンを押すと、下のプロバイダー選択が出てくるので最初に作成したプロバイダーを選択
スクリーンショット 2020-01-11 23.04.19.png
  1. プロバイダーの修正をするので、Developersの方へ戻る
スクリーンショット 2020-01-11 23.04.11.png
  1. 「リンクされたボット」を編集すると、先ほど作成したOAが選択できるようになるので選択
スクリーンショット 2020-01-11 23.05.09.png

最後に

今後やりたいことは、

  • LIFFを発展させる
  • OAでできることを全部試す
  • 友達や周りの人に使ってもらう

このようなことに挑戦して行きたいです!

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?