Q&A

GAS liff.init()が成功しているか確認したい。

これまでメールでやり取りしている社内の報告をLINE BOTとGASを利用して簡易化できないか試みています。 ※ GASならGmailに連携できるので

GAS上に作成したindex.html<head>部に以下のとおり<script>を埋め込んでおり、 <body>内に定義した<button id="sendbutton">がクリックされることでliff.sendMessagesを実行したいと考えています。

LIFF自体は表示されて<button>も表示されますが、クリックしても無反応となり、解析方法を悩んでいます。(もちろんトークに投稿されていません)。 確認ポイントについてコメントいただけると幸いです。 ※liffIdは伏せています。

    <script src="https://static.line-scdn.net/liff/edge/2/sdk.js"></script>
    <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", () => {
      liff
        .init({
          liffId: "********"
        })
        .then(() => {
          document.getElementById("sendbutton").addEventListener('click', () => {
            if (!liff.isInClient()) {
              window.alert('送信失敗!code:001');
            } else {
              liff.sendMessages([{
                'type': 'text',
                'text': 'これはテストメッセージです'
              }]).then(function() {
                window.alert('送信完了!');
              }).catch(function(error) {
                window.alert('送信失敗!' + error);
              });
            }
          });
        })
        .catch((err) => {
          window.alert('初期化失敗!');
        });
    });
    </script>

参考サイト様:【LIFF v2】メッセージ送信のサンプル【LINE】 via CROSS POWER(2020-06-03)

  • 0
  • 6
  • 6800
  • twitter facebook

Thank you for the advice.

First, I opened the liff url. Next, the result of running liff.login().

liff.login()
VM77:1 Uncaught ReferenceError: liff is not defined
    at <anonymous>:1:1

However, it works on heroku. LIFF started on heroku could also execute liff.sendmessages.

Or just post it. I can help you debug this problem.

How kind of you are!

But I'm sorry, I'm already using heroku. I hope to someday solve the problems with GAS for everyone else.

By the way, I heard this story. It seems that HTML created on GAS cannot embed JavaScript, and it is necessary to import an external .gs file. I don't have time to try it right now.

  • 0
Error: Need access_token for api call, Please login first

I saw this error message. Don't you login? Try this command at Chrome Console liff.login() After login and try send message again.

But the solution above is able when not using Line Client. Check your liff url. Or just post it. I can help you debug this problem.

  • 1

It was as I expected. I deployed the same code to heroku and it worked as expected.

I want the evolution of Google Apps Script.

thank you.

  • 0

I tried it. I used the extension LINE of Google Chrome. However, when I access it from Google Chrome, the in-app web browser cannot be used and the external browser starts. There is a restriction that liff.sendMessages cannot be used from external browsers. :-(

I am fixing the html so I will present it. <script> is written in<body>.

    <script>
    window.onload = function() {
      console.log('window onload start');
      initializeLiff("__MY_LIFF_ID__");
      console.log('window onload end');
    }

    function initializeLiff(myLiffId) {
      console.log('initializeLiff start');
      liff
        .init({
          liffId: myLiffId
        })
        .then(() => {
          console.log('sendMessages Load start');
          document.getElementById("sendbutton").addEventListener('click', () => {
           /*
					 test
           if (!liff.isInClient()) {
              console.error('sendError!! code:001');
            } else {
						*/
              liff.sendMessages([{
                type: 'text',
                text: 'this is test message.'
              }]).then(function() {
                console.log('send completed.');
              }).catch(function(error) {
                console.error('sendError!! code:002',error);
              });
						/*
            }
						*/
          });
          console.log('sendMessages Load completed.');
        })
        .catch((err) => {
          console.error('initializeLiffError!! code:003',err);
        });
      console.log('initializeLiff end');
    }
    </script>

console log here.

window onload start
userCodeAppPanel:9 initializeLiff start
userCodeAppPanel:35 initializeLiff end
userCodeAppPanel:5 window onload end
userCodeAppPanel:15 sendMessages Load start
userCodeAppPanel:30 sendMessages Load completed.
userCodeAppPanel:25 sendError!! code:002 Error: Need access_token for api call, Please login first
    at new t (sdk.js:2)
    at Q (sdk.js:2)
    at Ye (sdk.js:2)
    at Object.sendMessages (sdk.js:2)
    at HTMLButtonElement.<anonymous> (userCodeAppPanel:19)

I'm continuing to check if the same script works on heroku.

  • 0

That's a great idea. I was only checking in a LINE application running on a smartphone. I'm thinking of trying out Google Chrome developer tools. At the same time, I will consider vConsole.

I'm sorry, but I don't know when the results will come out. Because it is not my main business. I'll update this article when the results come out. Thank you very much.

  • 0

開発者ツールにエラーメッセージがありませんが? 見せでくれませんか?

  • 1
Are you sure? question.vm