|
@@ -0,0 +1,118 @@
|
|
|
+import copy
|
|
|
+import fastapi
|
|
|
+import fastapi.staticfiles as fastapiStaticfiles
|
|
|
+import linebot
|
|
|
+import linebot.models as linebotModels
|
|
|
+
|
|
|
+#
|
|
|
+
|
|
|
+from linebot.models import (
|
|
|
+ MessageEvent, TextMessage, TextSendMessage,
|
|
|
+ SourceUser, SourceGroup, SourceRoom,
|
|
|
+ TemplateSendMessage, ConfirmTemplate, MessageAction,
|
|
|
+ ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
|
|
|
+ PostbackAction, DatetimePickerAction,
|
|
|
+ CameraAction, CameraRollAction, LocationAction,
|
|
|
+ CarouselTemplate, CarouselColumn, PostbackEvent,
|
|
|
+ StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
|
|
|
+ ImageMessage, VideoMessage, AudioMessage, FileMessage,
|
|
|
+ UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
|
|
|
+ MemberJoinedEvent, MemberLeftEvent,
|
|
|
+ FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
|
|
|
+ TextComponent, IconComponent, ButtonComponent,
|
|
|
+ SeparatorComponent, QuickReply, QuickReplyButton,
|
|
|
+ ImageSendMessage)
|
|
|
+
|
|
|
+#uvicorn newbot:app --host 0.0.0.0 --port 5443 --ssl-keyfile=/etc/letsencrypt/live/api.ptt.cx/privkey.pem --ssl-certfile=/etc/letsencrypt/live/api.ptt.cx/fullchain.pem
|
|
|
+#uvicorn main:app --host 0.0.0.0 --port 443 --ssl-keyfile=/etc/letsencrypt/live/ptt.cx/privkey.pem --ssl-certfile=/etc/letsencrypt/live/ptt.cx/chain.pem
|
|
|
+
|
|
|
+#uvicorn main:app --host 0.0.0.0 --port 443 --key-file=/etc/letsencrypt/live/ptt.cx/privkey.pem --certfile=/etc/letsencrypt/live/ptt.cx/cert.pem
|
|
|
+
|
|
|
+# --keyfile=./key.pem --certfile=./cert.pem
|
|
|
+
|
|
|
+# --ssl-cert-reqs 1
|
|
|
+#
|
|
|
+# --ssl-ca-certs=/etc/letsencrypt/live/ptt.cx/fullchain.crt
|
|
|
+
|
|
|
+app = fastapi.FastAPI()
|
|
|
+app.mount(
|
|
|
+ '/static', fastapiStaticfiles.StaticFiles(directory='static'), name='static')
|
|
|
+
|
|
|
+
|
|
|
+from linebot import (
|
|
|
+ LineBotApi, WebhookHandler
|
|
|
+)
|
|
|
+from linebot.exceptions import (
|
|
|
+ InvalidSignatureError
|
|
|
+)
|
|
|
+from linebot.models import (
|
|
|
+ MessageEvent, TextMessage,ImageSendMessage, TextSendMessage,FlexSendMessage, TemplateSendMessage,CarouselTemplate,ConfirmTemplate,PostbackAction,MessageAction,CarouselColumn,URIAction
|
|
|
+)
|
|
|
+import json
|
|
|
+import codecs
|
|
|
+
|
|
|
+line_bot_api = LineBotApi('FvP2AkKj3FSuTjY8Jhwn23DPIb38yDZBliLrDjRA8j+UOQPku+kH2Bz2coDFl4gruRAiLuHuBbtRpILXlN54zJ+bWF5IwU86FClG47VZxCcDcAkhwqCqjYi/Ju7dBzlg963d9CV1KiiBwoiqvV6J7AdB04t89/1O/w1cDnyilFU=')
|
|
|
+handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
|
|
|
+#line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
|
|
|
+#handler = WebhookHandler('YOUR_CHANNEL_SECRET')
|
|
|
+
|
|
|
+
|
|
|
+@app.post('/callback')
|
|
|
+async def callback(request: fastapi.Request):
|
|
|
+ signature = request.headers['X-Line-Signature']
|
|
|
+ body = await request.body()
|
|
|
+ handler.handle(body.decode('utf-8'), signature)
|
|
|
+ return 'OK'
|
|
|
+
|
|
|
+
|
|
|
+@handler.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
|
|
|
+def message_text(event):
|
|
|
+ if event.message.text == 'push':
|
|
|
+ line_bot_api.push_message(
|
|
|
+ event.source.user_id, [
|
|
|
+ TextSendMessage(text='PUSH!'),
|
|
|
+ ]
|
|
|
+ )
|
|
|
+ if event.message.text == '叫':
|
|
|
+ line_bot_api.reply_message(
|
|
|
+ event.reply_token, linebotModels.AudioSendMessage(
|
|
|
+ original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
|
|
|
+ if event.message.text=='ok':
|
|
|
+ line_bot_api.reply_message(
|
|
|
+ event.reply_token,
|
|
|
+ TextSendMessage(text='ChoozMo 的FastAPI LINE Bot'))
|
|
|
+ if event.message.text=='btn':
|
|
|
+ line_bot_api.reply_message(
|
|
|
+ event.reply_token,
|
|
|
+ TextSendMessage(
|
|
|
+ text='Quick reply',
|
|
|
+ quick_reply=QuickReply(
|
|
|
+ items=[
|
|
|
+ QuickReplyButton(
|
|
|
+ action=PostbackAction(label="label1", data="data1")
|
|
|
+ ),
|
|
|
+ QuickReplyButton(
|
|
|
+ action=MessageAction(label="label2", text="text2")
|
|
|
+ ),
|
|
|
+ QuickReplyButton(
|
|
|
+ action=DatetimePickerAction(label="label3",
|
|
|
+ data="data3",
|
|
|
+ mode="date")
|
|
|
+ ),
|
|
|
+ QuickReplyButton(
|
|
|
+ action=CameraAction(label="label4")
|
|
|
+ ),
|
|
|
+ QuickReplyButton(
|
|
|
+ action=CameraRollAction(label="label5")
|
|
|
+ ),
|
|
|
+ QuickReplyButton(
|
|
|
+ action=LocationAction(label="label6")
|
|
|
+ ),
|
|
|
+ ])))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if __name__ == "__main__":
|
|
|
+# app.run(host='0.0.0.0', port=443,ssl_context=('/etc/letsencrypt/live/ptt.cx/fullchain.pem', '/etc/letsencrypt/live/ptt.cx/privkey.pem'))
|
|
|
+# app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))
|
|
|
+
|