LoginSignup
7
2

More than 1 year has passed since last update.

【初投稿】対話型ラインBotを作るには?

Last updated at Posted at 2021-11-02

目次

  • 最初に
  • 目標
  • LINE Notifyでは無理?
  • LINE messagingAPIを使おう!
  • herokuを使ってみる

1.最初に

自分のポートフォリオとして記事を残そうと思い、投稿を始めました。
Qiitaの書式のやり方におすすめなどアドバイスあれば教えていただきたいです。

2.目標

今回はLINEで混雑状況botを作ります。

3. LINE Notifyでは無理?

LINE Notifyを触ったことがなかったのでどういうものか調べ、pythonで簡易的なコードを書いてみました。

import requests
import datetime
import pytz

#取得したトークン・API
TOKEN='???'
api_url='https://notify-api.line.me/api/notify'

#時刻の処理
time=datetime.datetime.now(pytz.timezone('Asia/Tokyo'))
time=time.strftime('\n%Y年%m月%d日 %H時%M分%S秒')
send_message=time

#情報を辞書型にする
TOKEN_dic={'Authorization': 'Bearer'+ ' '+TOKEN}
send_dic={'message': send_message}

#LINE通知を送る(200: 成功 400:リクエストが不正 401:アクセストークンが無効)
requests.post(api_url,headers=TOKEN_dic,data=send_dic)

しかしこれではLINE Notify→自分しか通知がいかない!
ということで

4.LINE messagingAPIを使おう!

仮想環境を設定し、

LINE Developersでpythonのサンプルコードを入手、拡張し目標達成!

5.herokuを使ってみる

常時起動するために今回はherokuを使いました。(初心者)
セットアップの過程を示していきます。
まずはherokuをダウンロードします。
その前にherokuへ登録を登録はこちら

こちらはMacOSのダウンロード方法です。
その他は公式サイトに記載しています。
brew tap heroku/brew && brew install heroku
ここでエラーが!

###エラー文
Error:Your Command Line Tools are too outdated.
Update them from Software Update in System Preferences or run:
softwareupdate --all --install --force

If that doesn't show you any updates,run:
sudo rm -rf /Library/Developer/CommandLineTools
sudo Xcode-select --install

と表示されたので

  sudo Xcode-select --install

と実行するとかなり時間がかかりましたが無事herokuがインストールできました!

次に外部ライブラリをテキストに書き込みます。
その前に、herokuの上でwebサーバーとして動かすために準備、、
pip install gunicorn
とコマンドで動かします。
テキストに自動で書き込むためには、
pip freeze > requirments.txt
次にherokuでファイルを起動するために、Procfileを作ります。
中身はこんな感じ。
web: gunicorn app:app --log-file -

その後gitのダウンロードを行いました。

次にheroku login
でログインし
git init
heroku git:remote -a "herokuで作ったapp name"

残りはgitにadd,commit,pushするだけです。

デプロイ完了!

heroku公式サイトにわかりやすく説明が載っているので簡単でした。
python以外にも様々な言語の説明が載っています。
heroku pythonスターターガイドはこちら

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