123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import copy
- import fastapi
- import fastapi.staticfiles as fastapiStaticfiles
- 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
- #colors=[{'c1':}]
- ## json editor : https://jsonformatter.org/json-editor
- fr=codecs.open('/home/jared/src/pi/bubble.json','r','utf-8')
- lines=fr.readlines()
- content=' '.join(lines)
- fr.close()
- objelmt=json.loads(content)
- objelmt['body']['contents'][0]['contents'][0]['text']='this is a test'
- objelmt['header']['contents'][2]['contents'][0]['backgroundColor']='#0D8186'
- objelmt['header']['contents'][2]['backgroundColor']='#9FD8E36E'
- objelmt['header']['backgroundColor']='#27ACB2'
- ##7D51E4
- objelmt2=copy.deepcopy(objelmt)
- objelmt2['header']['contents'][2]['contents'][0]['backgroundColor']='#7D51E4'
- objelmt2['header']['contents'][2]['backgroundColor']='#9FD8E36E'
- objelmt2['header']['backgroundColor']='#A17DF5'
- ##7D51E4
- objelmt3=copy.deepcopy(objelmt)
- objelmt3['header']['contents'][2]['contents'][0]['backgroundColor']='#DE5658'
- objelmt3['header']['contents'][2]['backgroundColor']='#FAD2A76E'
- objelmt3['header']['backgroundColor']='#FF6B6E'
- objelmt4=copy.deepcopy(objelmt)
- objelmt4['header']['contents'][2]['contents'][0]['backgroundColor']='#0367D3'
- objelmt4['header']['contents'][2]['backgroundColor']='#FAD2A76E'
- objelmt4['header']['backgroundColor']='#2387F3'
- objelmt5=copy.deepcopy(objelmt)
- objelmt5['header']['contents'][2]['contents'][0]['backgroundColor']='#FFC107'
- objelmt5['header']['contents'][2]['backgroundColor']='#FAD2A76E'
- objelmt5['header']['backgroundColor']='#FFE127'
- objelmt6=copy.deepcopy(objelmt)
- objelmt6['header']['contents'][2]['contents'][0]['backgroundColor']='#343A40'
- objelmt6['header']['contents'][2]['backgroundColor']='#FAD2A76E'
- objelmt6['header']['backgroundColor']='#545A60'
- objelmt7=copy.deepcopy(objelmt)
- objelmt7['header']['contents'][2]['contents'][0]['backgroundColor']='#17A2B8'
- objelmt7['header']['contents'][2]['backgroundColor']='#FAD2A76E'
- objelmt7['header']['backgroundColor']='#37C2D8'
- jsobj={"type": "carousel", "contents": [objelmt,objelmt2,objelmt3,objelmt4,objelmt5,objelmt6,objelmt7]}
- 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()
- bot.handle(body.decode('utf-8'), signature)
- return 'OK'
- @bot.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
- def message_text(event):
- if event.message.text == '叫':
- lineBotApi.reply_message(
- event.reply_token, linebotModels.AudioSendMessage(
- original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
- #@app.route("/callback", methods=['POST','GET'])
- 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)
- print("Request body: " + body)
- # handle webhook body
- try:
- handler.handle(body, signature)
- except InvalidSignatureError:
- print("Invalid signature. Please check your channel access token/channel secret.")
- abort(400)
- return 'OK',200
- @app.route('/', methods=['GET'])
- def serve():
- return "Hello world", 200
- @handler.add(MessageEvent, message=TextMessage)
- def handle_message(event):
- print(event.message.text)
- if event.message.text=='業績':
- line_bot_api.reply_message(
- event.reply_token,
- TextSendMessage(text='您本月營收為9,287元'))
- if event.message.text=='新貨':
- line_bot_api.reply_message(
- event.reply_token,
- TextSendMessage(text='寶貝的毛巾該換囉! \
- 小朋友開學必備!家中必備! \
- 這款小方巾,不論是洗臉、嬰兒擦口水 \
- 或是帶去學校當手帕都很適合 '))
- if event.message.text=='防蚊':
- url='https://pic.pimg.tw/youwin721/1593748433-4270703895-g.png'
- line_bot_api.reply_message(
- event.reply_token,
- ImageSendMessage(url,url))
- if event.message.text=='華 地址':
- line_bot_api.reply_message(
- event.reply_token,
- TextSendMessage(text='林曉華的收件地址是: 110台北市信義區嘉興街235號'))
- if event.message.text=='AA':
- confirm_template_message = TemplateSendMessage(
- alt_text='Confirm template',
- template=ConfirmTemplate(
- text='Are you sure?',
- actions=[
- PostbackAction(
- label='postback',
- display_text='postback text',
- data='action=buy&itemid=1'
- ),
- MessageAction(
- label='message',
- text='message text'
- )
- ]
- )
- )
- line_bot_api.reply_message(event.reply_token,confirm_template_message)
- elif event.message.text=='cc':
- flex_message = FlexSendMessage(
- alt_text='hello',
- contents=jsobj
- )
- line_bot_api.reply_message(event.reply_token,flex_message)
- elif event.message.text=='BB':
- carousel_template_message = TemplateSendMessage(
- alt_text='Carousel template',
- template=CarouselTemplate(
- columns=[
- CarouselColumn(
- thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
- title='this is menu1',
- text='description1',
- actions=[
- PostbackAction(
- label='postback1',
- display_text='postback text1',
- data='action=buy&itemid=1'
- ),
- MessageAction(
- label='message1',
- text='message text1'
- ),
- URIAction(
- label='uri1',
- uri='http://example.com/1'
- )
- ]
- ),
- CarouselColumn(
- thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
- title='this is menu2',
- text='description2',
- actions=[
- PostbackAction(
- label='postback2',
- display_text='postback text2',
- data='action=buy&itemid=2'
- ),
- MessageAction(
- label='message2',
- text='message text2'
- ),
- URIAction(
- label='uri2',
- uri='http://example.com/2'
- )
- ]
- )
- ]
- )
- )
- line_bot_api.reply_message(event.reply_token,carousel_template_message)
- else:
- line_bot_api.reply_message(
- event.reply_token,
- TextSendMessage(text=event.message.text))
- 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' ))
|