0
0

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 3 years have passed since last update.

ID&PASS LINE bot

Last updated at Posted at 2021-09-14

ブログ記事はこちら
こちらを参考にさせていただきました。

from flask import Flask, request, abort
import pandas as pd

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)
import os


app = Flask(__name__)

#環境変数取得
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]

line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)

@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body)

    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)

    return 'OK'


@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    csv_path = './tecom_idpass.csv'
    student_number = event.message.text
    is_number = False
    reply_text = ""

    try:
        student_number = int(student_number)
        if student_number >= 5000 and student_number <= 5111:
            is_number = True
        else:
            reply_text = "整理番号が無効です。\n5001〜5111を入力してください。"
    except:
        reply_text = "整理番号が無効です。\n5001〜5111を入力してください。"

    if is_number:
        try:
            df = pd.read_csv(csv_path, encoding="shift-jis")
            data = df[df.number == student_number]
            name = data['name'].item()
        except:
            reply_text = "エラーが発生しました。"
        else:
            try:
                id = str(int(data['id'].item()))
                password = str(int(data['password'].item())).zfill(5)
                is_success = True
                reply_text = f"{student_number}\n{name}さん\n\n初期ID\n{id}\n\nパスワード\n{password}"
            except:
                reply_text = f"{student_number}\n{name}さん\n\nデータがありません。"

    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=reply_text))


if __name__ == "__main__":
#    app.run()
    port = int(os.getenv("PORT", 5000))
    app.run(host="0.0.0.0", port=port)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?