Spaces:
Sleeping
Sleeping
Commit
·
2fa1c78
1
Parent(s):
c8b1d94
learn
Browse files- Dockerfile +1 -1
- main.py +886 -20
Dockerfile
CHANGED
|
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
-
CMD ["gunicorn","-b", "0.0.0.0:7860", "main:app"]
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
+
CMD ["gunicorn","-b", "0.0.0.0:7860", "main:app"]
|
main.py
CHANGED
|
@@ -1,49 +1,157 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
| 3 |
import markdown
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
|
| 6 |
from linebot.v3 import (
|
| 7 |
WebhookHandler
|
| 8 |
)
|
|
|
|
|
|
|
|
|
|
| 9 |
from linebot.v3.exceptions import (
|
| 10 |
InvalidSignatureError
|
| 11 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from linebot.v3.messaging import (
|
| 13 |
Configuration,
|
| 14 |
ApiClient,
|
| 15 |
MessagingApi,
|
|
|
|
| 16 |
ReplyMessageRequest,
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
-
import os
|
| 25 |
import requests
|
| 26 |
|
| 27 |
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 28 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 29 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 30 |
def query(payload):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
configuration = Configuration(access_token='YOUR_CHANNEL_ACCESS_TOKEN')
|
| 37 |
-
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
|
| 38 |
|
| 39 |
@app.route("/")
|
| 40 |
def home():
|
| 41 |
return {"message": "Line Webhook Server"}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
@app.route("/callback", methods=['POST'])
|
| 44 |
def callback():
|
| 45 |
# get X-Line-Signature header value
|
| 46 |
-
signature = request.headers['
|
| 47 |
|
| 48 |
# get request body as text
|
| 49 |
body = request.get_data(as_text=True)
|
|
@@ -52,27 +160,785 @@ def callback():
|
|
| 52 |
# handle webhook body
|
| 53 |
try:
|
| 54 |
handler.handle(body, signature)
|
|
|
|
|
|
|
| 55 |
except InvalidSignatureError:
|
| 56 |
-
app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
|
| 57 |
abort(400)
|
| 58 |
|
| 59 |
return 'OK'
|
| 60 |
|
|
|
|
| 61 |
@handler.add(MessageEvent, message=TextMessageContent)
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
with ApiClient(configuration) as api_client:
|
| 64 |
line_bot_api = MessagingApi(api_client)
|
| 65 |
-
response = query({"inputs": input, "parameters":{"return_full_text":False, "max_length":1024}})
|
| 66 |
-
html_msg = markdown.markdown(response)
|
| 67 |
-
soup = BeautifulSoup(html_msg, 'html.parser')
|
| 68 |
line_bot_api.reply_message(
|
| 69 |
ReplyMessageRequest(
|
| 70 |
reply_token=event.reply_token,
|
| 71 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
)
|
| 74 |
|
| 75 |
-
if __name__ == "__main__":
|
| 76 |
-
app.run()
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import errno
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import logging
|
| 6 |
+
import tempfile
|
| 7 |
+
from argparse import ArgumentParser
|
| 8 |
|
| 9 |
+
from flask import Flask, request, abort, send_from_directory
|
| 10 |
+
# import google.generativeai as genai
|
| 11 |
import markdown
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
|
| 14 |
from linebot.v3 import (
|
| 15 |
WebhookHandler
|
| 16 |
)
|
| 17 |
+
from linebot.v3.models import (
|
| 18 |
+
UnknownEvent
|
| 19 |
+
)
|
| 20 |
from linebot.v3.exceptions import (
|
| 21 |
InvalidSignatureError
|
| 22 |
)
|
| 23 |
+
from linebot.v3.webhooks import (
|
| 24 |
+
MessageEvent,
|
| 25 |
+
TextMessageContent,
|
| 26 |
+
LocationMessageContent,
|
| 27 |
+
StickerMessageContent,
|
| 28 |
+
ImageMessageContent,
|
| 29 |
+
VideoMessageContent,
|
| 30 |
+
AudioMessageContent,
|
| 31 |
+
FileMessageContent,
|
| 32 |
+
UserSource,
|
| 33 |
+
RoomSource,
|
| 34 |
+
GroupSource,
|
| 35 |
+
FollowEvent,
|
| 36 |
+
UnfollowEvent,
|
| 37 |
+
JoinEvent,
|
| 38 |
+
LeaveEvent,
|
| 39 |
+
PostbackEvent,
|
| 40 |
+
BeaconEvent,
|
| 41 |
+
MemberJoinedEvent,
|
| 42 |
+
MemberLeftEvent,
|
| 43 |
+
)
|
| 44 |
from linebot.v3.messaging import (
|
| 45 |
Configuration,
|
| 46 |
ApiClient,
|
| 47 |
MessagingApi,
|
| 48 |
+
MessagingApiBlob,
|
| 49 |
ReplyMessageRequest,
|
| 50 |
+
PushMessageRequest,
|
| 51 |
+
MulticastRequest,
|
| 52 |
+
BroadcastRequest,
|
| 53 |
+
TextMessage,
|
| 54 |
+
ApiException,
|
| 55 |
+
LocationMessage,
|
| 56 |
+
StickerMessage,
|
| 57 |
+
ImageMessage,
|
| 58 |
+
TemplateMessage,
|
| 59 |
+
FlexMessage,
|
| 60 |
+
Emoji,
|
| 61 |
+
QuickReply,
|
| 62 |
+
QuickReplyItem,
|
| 63 |
+
ConfirmTemplate,
|
| 64 |
+
ButtonsTemplate,
|
| 65 |
+
CarouselTemplate,
|
| 66 |
+
CarouselColumn,
|
| 67 |
+
ImageCarouselTemplate,
|
| 68 |
+
ImageCarouselColumn,
|
| 69 |
+
FlexBubble,
|
| 70 |
+
FlexImage,
|
| 71 |
+
FlexBox,
|
| 72 |
+
FlexText,
|
| 73 |
+
FlexIcon,
|
| 74 |
+
FlexButton,
|
| 75 |
+
FlexSeparator,
|
| 76 |
+
FlexContainer,
|
| 77 |
+
MessageAction,
|
| 78 |
+
URIAction,
|
| 79 |
+
PostbackAction,
|
| 80 |
+
DatetimePickerAction,
|
| 81 |
+
CameraAction,
|
| 82 |
+
CameraRollAction,
|
| 83 |
+
LocationAction,
|
| 84 |
+
ErrorResponse
|
| 85 |
)
|
| 86 |
+
|
| 87 |
+
from linebot.v3.insight import (
|
| 88 |
+
ApiClient as InsightClient,
|
| 89 |
+
Insight
|
| 90 |
)
|
| 91 |
|
|
|
|
| 92 |
import requests
|
| 93 |
|
| 94 |
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 95 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 96 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 97 |
def query(payload):
|
| 98 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 99 |
+
return response.json()
|
| 100 |
|
| 101 |
app = Flask(__name__)
|
| 102 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 103 |
+
app.logger.setLevel(logging.INFO)
|
| 104 |
+
|
| 105 |
+
# get channel_secret and channel_access_token from your environment variable
|
| 106 |
+
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
|
| 107 |
+
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
|
| 108 |
+
|
| 109 |
+
if channel_secret is None or channel_access_token is None:
|
| 110 |
+
print('Specify LINE_CHANNEL_SECRET and LINE_CHANNEL_ACCESS_TOKEN as environment variables.')
|
| 111 |
+
sys.exit(1)
|
| 112 |
+
|
| 113 |
+
handler = WebhookHandler(channel_secret)
|
| 114 |
+
|
| 115 |
+
static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
|
| 116 |
+
|
| 117 |
+
configuration = Configuration(
|
| 118 |
+
access_token=channel_access_token
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# genai.configure(api_key=os.getenv('GOOGLE_API_KEY', None))
|
| 122 |
+
# model = genai.GenerativeModel('gemini-pro')
|
| 123 |
+
# chats: dict[str, genai.ChatSession] = {}
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
# def get_chat(user_id: str) -> genai.ChatSession:
|
| 127 |
+
# if user_id in chats:
|
| 128 |
+
# return chats.get(user_id)
|
| 129 |
+
# else:
|
| 130 |
+
# chat = model.start_chat()
|
| 131 |
+
# chats[user_id] = chat
|
| 132 |
+
# return chat
|
| 133 |
|
|
|
|
|
|
|
| 134 |
|
| 135 |
@app.route("/")
|
| 136 |
def home():
|
| 137 |
return {"message": "Line Webhook Server"}
|
| 138 |
|
| 139 |
+
|
| 140 |
+
# function for create tmp dir for download content
|
| 141 |
+
def make_static_tmp_dir():
|
| 142 |
+
try:
|
| 143 |
+
os.makedirs(static_tmp_path)
|
| 144 |
+
except OSError as exc:
|
| 145 |
+
if exc.errno == errno.EEXIST and os.path.isdir(static_tmp_path):
|
| 146 |
+
pass
|
| 147 |
+
else:
|
| 148 |
+
raise
|
| 149 |
+
|
| 150 |
+
|
| 151 |
@app.route("/callback", methods=['POST'])
|
| 152 |
def callback():
|
| 153 |
# get X-Line-Signature header value
|
| 154 |
+
signature = request.headers['X-Line-Signature']
|
| 155 |
|
| 156 |
# get request body as text
|
| 157 |
body = request.get_data(as_text=True)
|
|
|
|
| 160 |
# handle webhook body
|
| 161 |
try:
|
| 162 |
handler.handle(body, signature)
|
| 163 |
+
except ApiException as e:
|
| 164 |
+
app.logger.warn("Got exception from LINE Messaging API: %s\n" % e.body)
|
| 165 |
except InvalidSignatureError:
|
|
|
|
| 166 |
abort(400)
|
| 167 |
|
| 168 |
return 'OK'
|
| 169 |
|
| 170 |
+
|
| 171 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 172 |
+
def handle_text_message(event):
|
| 173 |
+
text = event.message.text
|
| 174 |
+
with ApiClient(configuration) as api_client:
|
| 175 |
+
line_bot_api = MessagingApi(api_client)
|
| 176 |
+
if text == 'profile':
|
| 177 |
+
if isinstance(event.source, UserSource):
|
| 178 |
+
profile = line_bot_api.get_profile(user_id=event.source.user_id)
|
| 179 |
+
line_bot_api.reply_message(
|
| 180 |
+
ReplyMessageRequest(
|
| 181 |
+
reply_token=event.reply_token,
|
| 182 |
+
messages=[
|
| 183 |
+
TextMessage(text='Display name: ' + profile.display_name),
|
| 184 |
+
TextMessage(text='Status message: ' + str(profile.status_message))
|
| 185 |
+
]
|
| 186 |
+
)
|
| 187 |
+
)
|
| 188 |
+
else:
|
| 189 |
+
line_bot_api.reply_message(
|
| 190 |
+
ReplyMessageRequest(
|
| 191 |
+
reply_token=event.reply_token,
|
| 192 |
+
messages=[TextMessage(text="Bot can't use profile API without user ID")]
|
| 193 |
+
)
|
| 194 |
+
)
|
| 195 |
+
elif text == 'emojis':
|
| 196 |
+
emojis = [Emoji(index=0, product_id="5ac1bfd5040ab15980c9b435", emoji_id="001"),
|
| 197 |
+
Emoji(index=13, product_id="5ac1bfd5040ab15980c9b435", emoji_id="002")]
|
| 198 |
+
line_bot_api.reply_message(
|
| 199 |
+
ReplyMessageRequest(
|
| 200 |
+
reply_token=event.reply_token,
|
| 201 |
+
messages=[TextMessage(text='$ LINE emoji $', emojis=emojis)]
|
| 202 |
+
)
|
| 203 |
+
)
|
| 204 |
+
elif text == 'quota':
|
| 205 |
+
quota = line_bot_api.get_message_quota()
|
| 206 |
+
line_bot_api.reply_message(
|
| 207 |
+
ReplyMessageRequest(
|
| 208 |
+
reply_token=event.reply_token,
|
| 209 |
+
messages=[
|
| 210 |
+
TextMessage(text='type: ' + quota.type),
|
| 211 |
+
TextMessage(text='value: ' + str(quota.value))
|
| 212 |
+
]
|
| 213 |
+
)
|
| 214 |
+
)
|
| 215 |
+
elif text == 'quota_consumption':
|
| 216 |
+
quota_consumption = line_bot_api.get_message_quota_consumption()
|
| 217 |
+
line_bot_api.reply_message(
|
| 218 |
+
ReplyMessageRequest(
|
| 219 |
+
reply_token=event.reply_token,
|
| 220 |
+
messages=[
|
| 221 |
+
TextMessage(text='total usage: ' + str(quota_consumption.total_usage))
|
| 222 |
+
]
|
| 223 |
+
)
|
| 224 |
+
)
|
| 225 |
+
elif text == 'push':
|
| 226 |
+
line_bot_api.push_message(
|
| 227 |
+
PushMessageRequest(
|
| 228 |
+
to=event.source.user_id,
|
| 229 |
+
messages=[TextMessage(text='PUSH!')]
|
| 230 |
+
)
|
| 231 |
+
)
|
| 232 |
+
elif text == 'multicast':
|
| 233 |
+
line_bot_api.multicast(
|
| 234 |
+
MulticastRequest(
|
| 235 |
+
to=[event.source.user_id],
|
| 236 |
+
messages=[TextMessage(text="THIS IS A MULTICAST MESSAGE, but it's slower than PUSH.")]
|
| 237 |
+
)
|
| 238 |
+
)
|
| 239 |
+
elif text == 'broadcast':
|
| 240 |
+
line_bot_api.broadcast(
|
| 241 |
+
BroadcastRequest(
|
| 242 |
+
messages=[TextMessage(text='THIS IS A BROADCAST MESSAGE')]
|
| 243 |
+
)
|
| 244 |
+
)
|
| 245 |
+
elif text.startswith('broadcast '): # broadcast 20190505
|
| 246 |
+
date = text.split(' ')[1]
|
| 247 |
+
app.logger.info("Getting broadcast result: " + date)
|
| 248 |
+
result = line_bot_api.get_number_of_sent_broadcast_messages(var_date=date)
|
| 249 |
+
line_bot_api.reply_message(
|
| 250 |
+
ReplyMessageRequest(
|
| 251 |
+
reply_token=event.reply_token,
|
| 252 |
+
messages=[
|
| 253 |
+
TextMessage(text='Number of sent broadcast messages: ' + date),
|
| 254 |
+
TextMessage(text='status: ' + str(result.status)),
|
| 255 |
+
TextMessage(text='success: ' + str(result.success)),
|
| 256 |
+
]
|
| 257 |
+
)
|
| 258 |
+
)
|
| 259 |
+
elif text == 'bye':
|
| 260 |
+
if isinstance(event.source, GroupSource):
|
| 261 |
+
line_bot_api.reply_message(
|
| 262 |
+
ReplyMessageRequest(
|
| 263 |
+
reply_token=event.reply_token,
|
| 264 |
+
messages=[TextMessage(text="Leaving group")]
|
| 265 |
+
)
|
| 266 |
+
)
|
| 267 |
+
line_bot_api.leave_group(event.source.group_id)
|
| 268 |
+
elif isinstance(event.source, RoomSource):
|
| 269 |
+
line_bot_api.reply_message(
|
| 270 |
+
ReplyMessageRequest(
|
| 271 |
+
reply_token=event.reply_token,
|
| 272 |
+
messages=[TextMessage(text="Leaving room")]
|
| 273 |
+
)
|
| 274 |
+
)
|
| 275 |
+
line_bot_api.leave_room(room_id=event.source.room_id)
|
| 276 |
+
else:
|
| 277 |
+
line_bot_api.reply_message(
|
| 278 |
+
ReplyMessageRequest(
|
| 279 |
+
reply_token=event.reply_token,
|
| 280 |
+
messages=[
|
| 281 |
+
TextMessage(text="Bot can't leave from 1:1 chat")
|
| 282 |
+
]
|
| 283 |
+
)
|
| 284 |
+
)
|
| 285 |
+
elif text == 'image':
|
| 286 |
+
url = 'https://placehold.co/400'
|
| 287 |
+
app.logger.info("url=" + url)
|
| 288 |
+
line_bot_api.reply_message(
|
| 289 |
+
ReplyMessageRequest(
|
| 290 |
+
reply_token=event.reply_token,
|
| 291 |
+
messages=[
|
| 292 |
+
ImageMessage(original_content_url=url, preview_image_url=url)
|
| 293 |
+
]
|
| 294 |
+
)
|
| 295 |
+
)
|
| 296 |
+
elif text == 'confirm':
|
| 297 |
+
confirm_template = ConfirmTemplate(
|
| 298 |
+
text='Do it?',
|
| 299 |
+
actions=[
|
| 300 |
+
MessageAction(label='Yes', text='Yes!'),
|
| 301 |
+
MessageAction(label='No', text='No!')
|
| 302 |
+
]
|
| 303 |
+
)
|
| 304 |
+
template_message = TemplateMessage(
|
| 305 |
+
alt_text='Confirm alt text',
|
| 306 |
+
template=confirm_template
|
| 307 |
+
)
|
| 308 |
+
line_bot_api.reply_message(
|
| 309 |
+
ReplyMessageRequest(
|
| 310 |
+
reply_token=event.reply_token,
|
| 311 |
+
messages=[template_message]
|
| 312 |
+
)
|
| 313 |
+
)
|
| 314 |
+
elif text == 'buttons':
|
| 315 |
+
buttons_template = ButtonsTemplate(
|
| 316 |
+
title='My buttons sample',
|
| 317 |
+
text='Hello, my buttons',
|
| 318 |
+
actions=[
|
| 319 |
+
URIAction(label='Go to line.me', uri='https://line.me'),
|
| 320 |
+
PostbackAction(label='ping', data='ping'),
|
| 321 |
+
PostbackAction(label='ping with text', data='ping', text='ping'),
|
| 322 |
+
MessageAction(label='Translate Rice', text='米')
|
| 323 |
+
])
|
| 324 |
+
template_message = TemplateMessage(
|
| 325 |
+
alt_text='Buttons alt text',
|
| 326 |
+
template=buttons_template
|
| 327 |
+
)
|
| 328 |
+
line_bot_api.reply_message(
|
| 329 |
+
ReplyMessageRequest(
|
| 330 |
+
reply_token=event.reply_token,
|
| 331 |
+
messages=[template_message]
|
| 332 |
+
)
|
| 333 |
+
)
|
| 334 |
+
elif text == 'carousel':
|
| 335 |
+
carousel_template = CarouselTemplate(
|
| 336 |
+
columns=[
|
| 337 |
+
CarouselColumn(
|
| 338 |
+
text='hoge1',
|
| 339 |
+
title='fuga1',
|
| 340 |
+
actions=[
|
| 341 |
+
URIAction(label='Go to line.me', uri='https://line.me'),
|
| 342 |
+
PostbackAction(label='ping', data='ping')
|
| 343 |
+
]
|
| 344 |
+
),
|
| 345 |
+
CarouselColumn(
|
| 346 |
+
text='hoge2',
|
| 347 |
+
title='fuga2',
|
| 348 |
+
actions=[
|
| 349 |
+
PostbackAction(label='ping with text', data='ping', text='ping'),
|
| 350 |
+
MessageAction(label='Translate Rice', text='米')
|
| 351 |
+
]
|
| 352 |
+
)
|
| 353 |
+
]
|
| 354 |
+
)
|
| 355 |
+
template_message = TemplateMessage(
|
| 356 |
+
alt_text='Carousel alt text', template=carousel_template)
|
| 357 |
+
line_bot_api.reply_message(
|
| 358 |
+
ReplyMessageRequest(
|
| 359 |
+
reply_token=event.reply_token,
|
| 360 |
+
messages=[template_message]
|
| 361 |
+
)
|
| 362 |
+
)
|
| 363 |
+
elif text == 'image_carousel':
|
| 364 |
+
image_carousel_template = ImageCarouselTemplate(columns=[
|
| 365 |
+
ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024',
|
| 366 |
+
action=DatetimePickerAction(label='datetime',
|
| 367 |
+
data='datetime_postback',
|
| 368 |
+
mode='datetime')),
|
| 369 |
+
ImageCarouselColumn(image_url='https://via.placeholder.com/1024x1024',
|
| 370 |
+
action=DatetimePickerAction(label='date',
|
| 371 |
+
data='date_postback',
|
| 372 |
+
mode='date'))
|
| 373 |
+
])
|
| 374 |
+
template_message = TemplateMessage(
|
| 375 |
+
alt_text='ImageCarousel alt text', template=image_carousel_template)
|
| 376 |
+
line_bot_api.reply_message(
|
| 377 |
+
ReplyMessageRequest(
|
| 378 |
+
reply_token=event.reply_token,
|
| 379 |
+
messages=[template_message]
|
| 380 |
+
)
|
| 381 |
+
)
|
| 382 |
+
elif text == 'imagemap':
|
| 383 |
+
pass
|
| 384 |
+
elif text == 'flex':
|
| 385 |
+
bubble = FlexBubble(
|
| 386 |
+
direction='ltr',
|
| 387 |
+
hero=FlexImage(
|
| 388 |
+
url='https://example.com/cafe.jpg',
|
| 389 |
+
size='full',
|
| 390 |
+
aspect_ratio='20:13',
|
| 391 |
+
aspect_mode='cover',
|
| 392 |
+
action=URIAction(uri='http://example.com', label='label')
|
| 393 |
+
),
|
| 394 |
+
body=FlexBox(
|
| 395 |
+
layout='vertical',
|
| 396 |
+
contents=[
|
| 397 |
+
# title
|
| 398 |
+
FlexText(text='Brown Cafe', weight='bold', size='xl'),
|
| 399 |
+
# review
|
| 400 |
+
FlexBox(
|
| 401 |
+
layout='baseline',
|
| 402 |
+
margin='md',
|
| 403 |
+
contents=[
|
| 404 |
+
FlexIcon(size='sm', url='https://example.com/gold_star.png'),
|
| 405 |
+
FlexIcon(size='sm', url='https://example.com/grey_star.png'),
|
| 406 |
+
FlexIcon(size='sm', url='https://example.com/gold_star.png'),
|
| 407 |
+
FlexIcon(size='sm', url='https://example.com/gold_star.png'),
|
| 408 |
+
FlexIcon(size='sm', url='https://example.com/grey_star.png'),
|
| 409 |
+
FlexText(text='4.0', size='sm', color='#999999', margin='md', flex=0)
|
| 410 |
+
]
|
| 411 |
+
),
|
| 412 |
+
# info
|
| 413 |
+
FlexBox(
|
| 414 |
+
layout='vertical',
|
| 415 |
+
margin='lg',
|
| 416 |
+
spacing='sm',
|
| 417 |
+
contents=[
|
| 418 |
+
FlexBox(
|
| 419 |
+
layout='baseline',
|
| 420 |
+
spacing='sm',
|
| 421 |
+
contents=[
|
| 422 |
+
FlexText(
|
| 423 |
+
text='Place',
|
| 424 |
+
color='#aaaaaa',
|
| 425 |
+
size='sm',
|
| 426 |
+
flex=1
|
| 427 |
+
),
|
| 428 |
+
FlexText(
|
| 429 |
+
text='Shinjuku, Tokyo',
|
| 430 |
+
wrap=True,
|
| 431 |
+
color='#666666',
|
| 432 |
+
size='sm',
|
| 433 |
+
flex=5
|
| 434 |
+
)
|
| 435 |
+
],
|
| 436 |
+
),
|
| 437 |
+
FlexBox(
|
| 438 |
+
layout='baseline',
|
| 439 |
+
spacing='sm',
|
| 440 |
+
contents=[
|
| 441 |
+
FlexText(
|
| 442 |
+
text='Time',
|
| 443 |
+
color='#aaaaaa',
|
| 444 |
+
size='sm',
|
| 445 |
+
flex=1
|
| 446 |
+
),
|
| 447 |
+
FlexText(
|
| 448 |
+
text="10:00 - 23:00",
|
| 449 |
+
wrap=True,
|
| 450 |
+
color='#666666',
|
| 451 |
+
size='sm',
|
| 452 |
+
flex=5,
|
| 453 |
+
),
|
| 454 |
+
],
|
| 455 |
+
),
|
| 456 |
+
],
|
| 457 |
+
)
|
| 458 |
+
],
|
| 459 |
+
),
|
| 460 |
+
footer=FlexBox(
|
| 461 |
+
layout='vertical',
|
| 462 |
+
spacing='sm',
|
| 463 |
+
contents=[
|
| 464 |
+
# callAction
|
| 465 |
+
FlexButton(
|
| 466 |
+
style='link',
|
| 467 |
+
height='sm',
|
| 468 |
+
action=URIAction(label='CALL', uri='tel:000000'),
|
| 469 |
+
),
|
| 470 |
+
# separator
|
| 471 |
+
FlexSeparator(),
|
| 472 |
+
# websiteAction
|
| 473 |
+
FlexButton(
|
| 474 |
+
style='link',
|
| 475 |
+
height='sm',
|
| 476 |
+
action=URIAction(label='WEBSITE', uri="https://example.com")
|
| 477 |
+
)
|
| 478 |
+
]
|
| 479 |
+
),
|
| 480 |
+
)
|
| 481 |
+
line_bot_api.reply_message(
|
| 482 |
+
ReplyMessageRequest(
|
| 483 |
+
reply_token=event.reply_token,
|
| 484 |
+
messages=[FlexMessage(alt_text="hello", contents=bubble)]
|
| 485 |
+
)
|
| 486 |
+
)
|
| 487 |
+
elif text == 'flex_update_1':
|
| 488 |
+
bubble_string = """
|
| 489 |
+
{
|
| 490 |
+
"type": "bubble",
|
| 491 |
+
"body": {
|
| 492 |
+
"type": "box",
|
| 493 |
+
"layout": "vertical",
|
| 494 |
+
"contents": [
|
| 495 |
+
{
|
| 496 |
+
"type": "image",
|
| 497 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip3.jpg",
|
| 498 |
+
"position": "relative",
|
| 499 |
+
"size": "full",
|
| 500 |
+
"aspectMode": "cover",
|
| 501 |
+
"aspectRatio": "1:1",
|
| 502 |
+
"gravity": "center"
|
| 503 |
+
},
|
| 504 |
+
{
|
| 505 |
+
"type": "box",
|
| 506 |
+
"layout": "horizontal",
|
| 507 |
+
"contents": [
|
| 508 |
+
{
|
| 509 |
+
"type": "box",
|
| 510 |
+
"layout": "vertical",
|
| 511 |
+
"contents": [
|
| 512 |
+
{
|
| 513 |
+
"type": "text",
|
| 514 |
+
"text": "Brown Hotel",
|
| 515 |
+
"weight": "bold",
|
| 516 |
+
"size": "xl",
|
| 517 |
+
"color": "#ffffff"
|
| 518 |
+
},
|
| 519 |
+
{
|
| 520 |
+
"type": "box",
|
| 521 |
+
"layout": "baseline",
|
| 522 |
+
"margin": "md",
|
| 523 |
+
"contents": [
|
| 524 |
+
{
|
| 525 |
+
"type": "icon",
|
| 526 |
+
"size": "sm",
|
| 527 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png"
|
| 528 |
+
},
|
| 529 |
+
{
|
| 530 |
+
"type": "icon",
|
| 531 |
+
"size": "sm",
|
| 532 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png"
|
| 533 |
+
},
|
| 534 |
+
{
|
| 535 |
+
"type": "icon",
|
| 536 |
+
"size": "sm",
|
| 537 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png"
|
| 538 |
+
},
|
| 539 |
+
{
|
| 540 |
+
"type": "icon",
|
| 541 |
+
"size": "sm",
|
| 542 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png"
|
| 543 |
+
},
|
| 544 |
+
{
|
| 545 |
+
"type": "icon",
|
| 546 |
+
"size": "sm",
|
| 547 |
+
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png"
|
| 548 |
+
},
|
| 549 |
+
{
|
| 550 |
+
"type": "text",
|
| 551 |
+
"text": "4.0",
|
| 552 |
+
"size": "sm",
|
| 553 |
+
"color": "#d6d6d6",
|
| 554 |
+
"margin": "md",
|
| 555 |
+
"flex": 0
|
| 556 |
+
}
|
| 557 |
+
]
|
| 558 |
+
}
|
| 559 |
+
]
|
| 560 |
+
},
|
| 561 |
+
{
|
| 562 |
+
"type": "box",
|
| 563 |
+
"layout": "vertical",
|
| 564 |
+
"contents": [
|
| 565 |
+
{
|
| 566 |
+
"type": "text",
|
| 567 |
+
"text": "¥62,000",
|
| 568 |
+
"color": "#a9a9a9",
|
| 569 |
+
"decoration": "line-through",
|
| 570 |
+
"align": "end"
|
| 571 |
+
},
|
| 572 |
+
{
|
| 573 |
+
"type": "text",
|
| 574 |
+
"text": "¥42,000",
|
| 575 |
+
"color": "#ebebeb",
|
| 576 |
+
"size": "xl",
|
| 577 |
+
"align": "end"
|
| 578 |
+
}
|
| 579 |
+
]
|
| 580 |
+
}
|
| 581 |
+
],
|
| 582 |
+
"position": "absolute",
|
| 583 |
+
"offsetBottom": "0px",
|
| 584 |
+
"offsetStart": "0px",
|
| 585 |
+
"offsetEnd": "0px",
|
| 586 |
+
"backgroundColor": "#00000099",
|
| 587 |
+
"paddingAll": "20px"
|
| 588 |
+
},
|
| 589 |
+
{
|
| 590 |
+
"type": "box",
|
| 591 |
+
"layout": "vertical",
|
| 592 |
+
"contents": [
|
| 593 |
+
{
|
| 594 |
+
"type": "text",
|
| 595 |
+
"text": "SALE",
|
| 596 |
+
"color": "#ffffff"
|
| 597 |
+
}
|
| 598 |
+
],
|
| 599 |
+
"position": "absolute",
|
| 600 |
+
"backgroundColor": "#ff2600",
|
| 601 |
+
"cornerRadius": "20px",
|
| 602 |
+
"paddingAll": "5px",
|
| 603 |
+
"offsetTop": "10px",
|
| 604 |
+
"offsetEnd": "10px",
|
| 605 |
+
"paddingStart": "10px",
|
| 606 |
+
"paddingEnd": "10px"
|
| 607 |
+
}
|
| 608 |
+
],
|
| 609 |
+
"paddingAll": "0px"
|
| 610 |
+
}
|
| 611 |
+
}
|
| 612 |
+
"""
|
| 613 |
+
message = FlexMessage(alt_text="hello", contents=FlexContainer.from_json(bubble_string))
|
| 614 |
+
line_bot_api.reply_message(
|
| 615 |
+
ReplyMessageRequest(
|
| 616 |
+
reply_token=event.reply_token,
|
| 617 |
+
messages=[message]
|
| 618 |
+
)
|
| 619 |
+
)
|
| 620 |
+
elif text == 'quick_reply':
|
| 621 |
+
line_bot_api.reply_message(
|
| 622 |
+
ReplyMessageRequest(
|
| 623 |
+
reply_token=event.reply_token,
|
| 624 |
+
messages=[TextMessage(
|
| 625 |
+
text='Quick reply',
|
| 626 |
+
quick_reply=QuickReply(
|
| 627 |
+
items=[
|
| 628 |
+
QuickReplyItem(
|
| 629 |
+
action=PostbackAction(label="label1", data="data1")
|
| 630 |
+
),
|
| 631 |
+
QuickReplyItem(
|
| 632 |
+
action=MessageAction(label="label2", text="text2")
|
| 633 |
+
),
|
| 634 |
+
QuickReplyItem(
|
| 635 |
+
action=DatetimePickerAction(label="label3",
|
| 636 |
+
data="data3",
|
| 637 |
+
mode="date")
|
| 638 |
+
),
|
| 639 |
+
QuickReplyItem(
|
| 640 |
+
action=CameraAction(label="label4")
|
| 641 |
+
),
|
| 642 |
+
QuickReplyItem(
|
| 643 |
+
action=CameraRollAction(label="label5")
|
| 644 |
+
),
|
| 645 |
+
QuickReplyItem(
|
| 646 |
+
action=LocationAction(label="label6")
|
| 647 |
+
),
|
| 648 |
+
]
|
| 649 |
+
)
|
| 650 |
+
)]
|
| 651 |
+
)
|
| 652 |
+
)
|
| 653 |
+
elif text == 'link_token' and isinstance(event.source, UserSource):
|
| 654 |
+
link_token_response = line_bot_api.issue_link_token(user_id=event.source.user_id)
|
| 655 |
+
line_bot_api.reply_message(
|
| 656 |
+
ReplyMessageRequest(
|
| 657 |
+
reply_token=event.reply_token,
|
| 658 |
+
messages=[TextMessage(text='link_token: ' + link_token_response.link_token)]
|
| 659 |
+
)
|
| 660 |
+
)
|
| 661 |
+
elif text == 'insight_message_delivery':
|
| 662 |
+
with InsightClient(configuration) as api_client:
|
| 663 |
+
line_bot_insight_api = Insight(api_client)
|
| 664 |
+
today = datetime.date.today().strftime("%Y%m%d")
|
| 665 |
+
response = line_bot_insight_api.get_number_of_message_deliveries(var_date=today)
|
| 666 |
+
if response.status == 'ready':
|
| 667 |
+
messages = [
|
| 668 |
+
TextMessage(text='broadcast: ' + str(response.broadcast)),
|
| 669 |
+
TextMessage(text='targeting: ' + str(response.targeting)),
|
| 670 |
+
]
|
| 671 |
+
else:
|
| 672 |
+
messages = [TextMessage(text='status: ' + response.status)]
|
| 673 |
+
line_bot_api.reply_message(
|
| 674 |
+
ReplyMessageRequest(
|
| 675 |
+
reply_token=event.reply_token,
|
| 676 |
+
messages=messages
|
| 677 |
+
)
|
| 678 |
+
)
|
| 679 |
+
elif text == 'insight_followers':
|
| 680 |
+
with InsightClient(configuration) as api_client:
|
| 681 |
+
line_bot_insight_api = Insight(api_client)
|
| 682 |
+
today = datetime.date.today().strftime("%Y%m%d")
|
| 683 |
+
response = line_bot_insight_api.get_number_of_followers(var_date=today)
|
| 684 |
+
if response.status == 'ready':
|
| 685 |
+
messages = [
|
| 686 |
+
TextMessage(text='followers: ' + str(response.followers)),
|
| 687 |
+
TextMessage(text='targetedReaches: ' + str(response.targeted_reaches)),
|
| 688 |
+
TextMessage(text='blocks: ' + str(response.blocks)),
|
| 689 |
+
]
|
| 690 |
+
else:
|
| 691 |
+
messages = [TextMessage(text='status: ' + response.status)]
|
| 692 |
+
line_bot_api.reply_message(
|
| 693 |
+
ReplyMessageRequest(
|
| 694 |
+
reply_token=event.reply_token,
|
| 695 |
+
messages=messages
|
| 696 |
+
)
|
| 697 |
+
)
|
| 698 |
+
elif text == 'insight_demographic':
|
| 699 |
+
with InsightClient(configuration) as api_client:
|
| 700 |
+
line_bot_insight_api = Insight(api_client)
|
| 701 |
+
response = line_bot_insight_api.get_friends_demographics()
|
| 702 |
+
if response.available:
|
| 703 |
+
messages = ["{gender}: {percentage}".format(gender=it.gender, percentage=it.percentage)
|
| 704 |
+
for it in response.genders]
|
| 705 |
+
else:
|
| 706 |
+
messages = [TextMessage(text='available: false')]
|
| 707 |
+
line_bot_api.reply_message(
|
| 708 |
+
ReplyMessageRequest(
|
| 709 |
+
reply_token=event.reply_token,
|
| 710 |
+
messages=messages
|
| 711 |
+
)
|
| 712 |
+
)
|
| 713 |
+
elif text == 'with http info':
|
| 714 |
+
response = line_bot_api.reply_message_with_http_info(
|
| 715 |
+
ReplyMessageRequest(
|
| 716 |
+
reply_token=event.reply_token,
|
| 717 |
+
messages=[TextMessage(text='see application log')]
|
| 718 |
+
)
|
| 719 |
+
)
|
| 720 |
+
app.logger.info("Got response with http status code: " + str(response.status_code))
|
| 721 |
+
app.logger.info("Got x-line-request-id: " + response.headers['x-line-request-id'])
|
| 722 |
+
app.logger.info("Got response with http body: " + str(response.data))
|
| 723 |
+
elif text == 'with http info error':
|
| 724 |
+
try:
|
| 725 |
+
line_bot_api.reply_message_with_http_info(
|
| 726 |
+
ReplyMessageRequest(
|
| 727 |
+
reply_token='invalid-reply-token',
|
| 728 |
+
messages=[TextMessage(text='see application log')]
|
| 729 |
+
)
|
| 730 |
+
)
|
| 731 |
+
except ApiException as e:
|
| 732 |
+
app.logger.info("Got response with http status code: " + str(e.status))
|
| 733 |
+
app.logger.info("Got x-line-request-id: " + e.headers['x-line-request-id'])
|
| 734 |
+
app.logger.info("Got response with http body: " + str(ErrorResponse.from_json(e.body)))
|
| 735 |
+
else:
|
| 736 |
+
# chat = get_chat(event.source.user_id)
|
| 737 |
+
# response = chat.send_message(text).text
|
| 738 |
+
response = query({"inputs": input,"parameters":{"return_full_text":False,"max_length":1024}})
|
| 739 |
+
html_msg = markdown.markdown(response)
|
| 740 |
+
soup = BeautifulSoup(html_msg, 'html.parser')
|
| 741 |
+
line_bot_api.reply_message(
|
| 742 |
+
ReplyMessageRequest(
|
| 743 |
+
reply_token=event.reply_token,
|
| 744 |
+
messages=[TextMessage(text=soup.get_text())]
|
| 745 |
+
)
|
| 746 |
+
)
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
@handler.add(MessageEvent, message=LocationMessageContent)
|
| 750 |
+
def handle_location_message(event):
|
| 751 |
+
with ApiClient(configuration) as api_client:
|
| 752 |
+
line_bot_api = MessagingApi(api_client)
|
| 753 |
+
line_bot_api.reply_message(
|
| 754 |
+
ReplyMessageRequest(
|
| 755 |
+
reply_token=event.reply_token,
|
| 756 |
+
messages=[LocationMessage(
|
| 757 |
+
title='Location',
|
| 758 |
+
address=event.message.address,
|
| 759 |
+
latitude=event.message.latitude,
|
| 760 |
+
longitude=event.message.longitude
|
| 761 |
+
)]
|
| 762 |
+
)
|
| 763 |
+
)
|
| 764 |
+
|
| 765 |
+
|
| 766 |
+
@handler.add(MessageEvent, message=StickerMessageContent)
|
| 767 |
+
def handle_sticker_message(event):
|
| 768 |
+
with ApiClient(configuration) as api_client:
|
| 769 |
+
line_bot_api = MessagingApi(api_client)
|
| 770 |
+
line_bot_api.reply_message(
|
| 771 |
+
ReplyMessageRequest(
|
| 772 |
+
reply_token=event.reply_token,
|
| 773 |
+
messages=[StickerMessage(
|
| 774 |
+
package_id=event.message.package_id,
|
| 775 |
+
sticker_id=event.message.sticker_id)
|
| 776 |
+
]
|
| 777 |
+
)
|
| 778 |
+
)
|
| 779 |
+
|
| 780 |
+
|
| 781 |
+
# Other Message Type
|
| 782 |
+
@handler.add(MessageEvent, message=(ImageMessageContent,
|
| 783 |
+
VideoMessageContent,
|
| 784 |
+
AudioMessageContent))
|
| 785 |
+
def handle_content_message(event):
|
| 786 |
+
if isinstance(event.message, ImageMessageContent):
|
| 787 |
+
ext = 'jpg'
|
| 788 |
+
elif isinstance(event.message, VideoMessageContent):
|
| 789 |
+
ext = 'mp4'
|
| 790 |
+
elif isinstance(event.message, AudioMessageContent):
|
| 791 |
+
ext = 'm4a'
|
| 792 |
+
else:
|
| 793 |
+
return
|
| 794 |
+
|
| 795 |
+
with ApiClient(configuration) as api_client:
|
| 796 |
+
line_bot_blob_api = MessagingApiBlob(api_client)
|
| 797 |
+
message_content = line_bot_blob_api.get_message_content(message_id=event.message.id)
|
| 798 |
+
with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix=ext + '-', delete=False) as tf:
|
| 799 |
+
tf.write(message_content)
|
| 800 |
+
tempfile_path = tf.name
|
| 801 |
+
|
| 802 |
+
dist_path = tempfile_path + '.' + ext
|
| 803 |
+
dist_name = os.path.basename(dist_path)
|
| 804 |
+
os.rename(tempfile_path, dist_path)
|
| 805 |
+
|
| 806 |
+
with ApiClient(configuration) as api_client:
|
| 807 |
+
line_bot_api = MessagingApi(api_client)
|
| 808 |
+
line_bot_api.reply_message(
|
| 809 |
+
ReplyMessageRequest(
|
| 810 |
+
reply_token=event.reply_token,
|
| 811 |
+
messages=[
|
| 812 |
+
TextMessage(text='Save content.'),
|
| 813 |
+
TextMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
|
| 814 |
+
]
|
| 815 |
+
)
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
+
|
| 819 |
+
@handler.add(MessageEvent, message=FileMessageContent)
|
| 820 |
+
def handle_file_message(event):
|
| 821 |
+
with ApiClient(configuration) as api_client:
|
| 822 |
+
line_bot_blob_api = MessagingApiBlob(api_client)
|
| 823 |
+
message_content = line_bot_blob_api.get_message_content(message_id=event.message.id)
|
| 824 |
+
with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix='file-', delete=False) as tf:
|
| 825 |
+
tf.write(message_content)
|
| 826 |
+
tempfile_path = tf.name
|
| 827 |
+
|
| 828 |
+
dist_path = tempfile_path + '-' + event.message.file_name
|
| 829 |
+
dist_name = os.path.basename(dist_path)
|
| 830 |
+
os.rename(tempfile_path, dist_path)
|
| 831 |
+
|
| 832 |
with ApiClient(configuration) as api_client:
|
| 833 |
line_bot_api = MessagingApi(api_client)
|
|
|
|
|
|
|
|
|
|
| 834 |
line_bot_api.reply_message(
|
| 835 |
ReplyMessageRequest(
|
| 836 |
reply_token=event.reply_token,
|
| 837 |
+
messages=[
|
| 838 |
+
TextMessage(text='Save file.'),
|
| 839 |
+
TextMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
|
| 840 |
+
]
|
| 841 |
)
|
| 842 |
)
|
| 843 |
|
|
|
|
|
|
|
| 844 |
|
| 845 |
+
@handler.add(FollowEvent)
|
| 846 |
+
def handle_follow(event):
|
| 847 |
+
app.logger.info("Got Follow event:" + event.source.user_id)
|
| 848 |
+
with ApiClient(configuration) as api_client:
|
| 849 |
+
line_bot_api = MessagingApi(api_client)
|
| 850 |
+
line_bot_api.reply_message(
|
| 851 |
+
ReplyMessageRequest(
|
| 852 |
+
reply_token=event.reply_token,
|
| 853 |
+
messages=[TextMessage(text='Got follow event')]
|
| 854 |
+
)
|
| 855 |
+
)
|
| 856 |
+
|
| 857 |
+
|
| 858 |
+
@handler.add(UnfollowEvent)
|
| 859 |
+
def handle_unfollow(event):
|
| 860 |
+
app.logger.info("Got Unfollow event:" + event.source.user_id)
|
| 861 |
+
|
| 862 |
+
|
| 863 |
+
@handler.add(JoinEvent)
|
| 864 |
+
def handle_join(event):
|
| 865 |
+
with ApiClient(configuration) as api_client:
|
| 866 |
+
line_bot_api = MessagingApi(api_client)
|
| 867 |
+
line_bot_api.reply_message(
|
| 868 |
+
ReplyMessageRequest(
|
| 869 |
+
reply_token=event.reply_token,
|
| 870 |
+
messages=[TextMessage(text='Joined this ' + event.source.type)]
|
| 871 |
+
)
|
| 872 |
+
)
|
| 873 |
+
|
| 874 |
+
|
| 875 |
+
@handler.add(LeaveEvent)
|
| 876 |
+
def handle_leave():
|
| 877 |
+
app.logger.info("Got leave event")
|
| 878 |
+
|
| 879 |
+
|
| 880 |
+
@handler.add(PostbackEvent)
|
| 881 |
+
def handle_postback(event: PostbackEvent):
|
| 882 |
+
with ApiClient(configuration) as api_client:
|
| 883 |
+
line_bot_api = MessagingApi(api_client)
|
| 884 |
+
if event.postback.data == 'ping':
|
| 885 |
+
line_bot_api.reply_message(
|
| 886 |
+
ReplyMessageRequest(
|
| 887 |
+
reply_token=event.reply_token,
|
| 888 |
+
messages=[TextMessage(text='pong')]
|
| 889 |
+
)
|
| 890 |
+
)
|
| 891 |
+
elif event.postback.data == 'datetime_postback':
|
| 892 |
+
line_bot_api.reply_message(
|
| 893 |
+
ReplyMessageRequest(
|
| 894 |
+
reply_token=event.reply_token,
|
| 895 |
+
messages=[TextMessage(text=event.postback.params['datetime'])]
|
| 896 |
+
)
|
| 897 |
+
)
|
| 898 |
+
elif event.postback.data == 'date_postback':
|
| 899 |
+
line_bot_api.reply_message(
|
| 900 |
+
ReplyMessageRequest(
|
| 901 |
+
reply_token=event.reply_token,
|
| 902 |
+
messages=[TextMessage(text=event.postback.params['date'])]
|
| 903 |
+
)
|
| 904 |
+
)
|
| 905 |
+
|
| 906 |
+
|
| 907 |
+
@handler.add(BeaconEvent)
|
| 908 |
+
def handle_beacon(event: BeaconEvent):
|
| 909 |
+
with ApiClient(configuration) as api_client:
|
| 910 |
+
line_bot_api = MessagingApi(api_client)
|
| 911 |
+
line_bot_api.reply_message(
|
| 912 |
+
ReplyMessageRequest(
|
| 913 |
+
reply_token=event.reply_token,
|
| 914 |
+
messages=[TextMessage(text='Got beacon event. hwid={}, device_message(hex string)={}'.format(
|
| 915 |
+
event.beacon.hwid, event.beacon.dm))]
|
| 916 |
+
)
|
| 917 |
+
)
|
| 918 |
+
|
| 919 |
+
|
| 920 |
+
@handler.add(MemberJoinedEvent)
|
| 921 |
+
def handle_member_joined(event):
|
| 922 |
+
with ApiClient(configuration) as api_client:
|
| 923 |
+
line_bot_api = MessagingApi(api_client)
|
| 924 |
+
line_bot_api.reply_message(
|
| 925 |
+
ReplyMessageRequest(
|
| 926 |
+
reply_token=event.reply_token,
|
| 927 |
+
messages=[TextMessage(text='Got memberJoined event. event={}'.format(event))]
|
| 928 |
+
)
|
| 929 |
+
)
|
| 930 |
+
|
| 931 |
+
|
| 932 |
+
@handler.add(MemberLeftEvent)
|
| 933 |
+
def handle_member_left(event):
|
| 934 |
+
app.logger.info("Got memberLeft event")
|
| 935 |
+
|
| 936 |
+
|
| 937 |
+
@handler.add(UnknownEvent)
|
| 938 |
+
def handle_unknown_left(event):
|
| 939 |
+
app.logger.info(f"unknown event {event}")
|
| 940 |
+
|
| 941 |
+
|
| 942 |
+
@app.route('/static/<path:path>')
|
| 943 |
+
def send_static_content(path):
|
| 944 |
+
return send_from_directory('static', path)
|