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)
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme