jbot.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import copy
  2. import fastapi
  3. import fastapi.staticfiles as fastapiStaticfiles
  4. app = fastapi.FastAPI()
  5. app.mount(
  6. '/static', fastapiStaticfiles.StaticFiles(directory='static'), name='static')
  7. from linebot import (
  8. LineBotApi, WebhookHandler
  9. )
  10. from linebot.exceptions import (
  11. InvalidSignatureError
  12. )
  13. from linebot.models import (
  14. MessageEvent, TextMessage,ImageSendMessage, TextSendMessage,FlexSendMessage, TemplateSendMessage,CarouselTemplate,ConfirmTemplate,PostbackAction,MessageAction,CarouselColumn,URIAction
  15. )
  16. import json
  17. import codecs
  18. #colors=[{'c1':}]
  19. ## json editor : https://jsonformatter.org/json-editor
  20. fr=codecs.open('/home/jared/src/pi/bubble.json','r','utf-8')
  21. lines=fr.readlines()
  22. content=' '.join(lines)
  23. fr.close()
  24. objelmt=json.loads(content)
  25. objelmt['body']['contents'][0]['contents'][0]['text']='this is a test'
  26. objelmt['header']['contents'][2]['contents'][0]['backgroundColor']='#0D8186'
  27. objelmt['header']['contents'][2]['backgroundColor']='#9FD8E36E'
  28. objelmt['header']['backgroundColor']='#27ACB2'
  29. ##7D51E4
  30. objelmt2=copy.deepcopy(objelmt)
  31. objelmt2['header']['contents'][2]['contents'][0]['backgroundColor']='#7D51E4'
  32. objelmt2['header']['contents'][2]['backgroundColor']='#9FD8E36E'
  33. objelmt2['header']['backgroundColor']='#A17DF5'
  34. ##7D51E4
  35. objelmt3=copy.deepcopy(objelmt)
  36. objelmt3['header']['contents'][2]['contents'][0]['backgroundColor']='#DE5658'
  37. objelmt3['header']['contents'][2]['backgroundColor']='#FAD2A76E'
  38. objelmt3['header']['backgroundColor']='#FF6B6E'
  39. objelmt4=copy.deepcopy(objelmt)
  40. objelmt4['header']['contents'][2]['contents'][0]['backgroundColor']='#0367D3'
  41. objelmt4['header']['contents'][2]['backgroundColor']='#FAD2A76E'
  42. objelmt4['header']['backgroundColor']='#2387F3'
  43. objelmt5=copy.deepcopy(objelmt)
  44. objelmt5['header']['contents'][2]['contents'][0]['backgroundColor']='#FFC107'
  45. objelmt5['header']['contents'][2]['backgroundColor']='#FAD2A76E'
  46. objelmt5['header']['backgroundColor']='#FFE127'
  47. objelmt6=copy.deepcopy(objelmt)
  48. objelmt6['header']['contents'][2]['contents'][0]['backgroundColor']='#343A40'
  49. objelmt6['header']['contents'][2]['backgroundColor']='#FAD2A76E'
  50. objelmt6['header']['backgroundColor']='#545A60'
  51. objelmt7=copy.deepcopy(objelmt)
  52. objelmt7['header']['contents'][2]['contents'][0]['backgroundColor']='#17A2B8'
  53. objelmt7['header']['contents'][2]['backgroundColor']='#FAD2A76E'
  54. objelmt7['header']['backgroundColor']='#37C2D8'
  55. jsobj={"type": "carousel", "contents": [objelmt,objelmt2,objelmt3,objelmt4,objelmt5,objelmt6,objelmt7]}
  56. line_bot_api = LineBotApi('FvP2AkKj3FSuTjY8Jhwn23DPIb38yDZBliLrDjRA8j+UOQPku+kH2Bz2coDFl4gruRAiLuHuBbtRpILXlN54zJ+bWF5IwU86FClG47VZxCcDcAkhwqCqjYi/Ju7dBzlg963d9CV1KiiBwoiqvV6J7AdB04t89/1O/w1cDnyilFU=')
  57. handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
  58. #line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
  59. #handler = WebhookHandler('YOUR_CHANNEL_SECRET')
  60. @app.post('/callback')
  61. async def callback(request: fastapi.Request):
  62. signature = request.headers['X-Line-Signature']
  63. body = await request.body()
  64. bot.handle(body.decode('utf-8'), signature)
  65. return 'OK'
  66. @bot.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
  67. def message_text(event):
  68. if event.message.text == '叫':
  69. lineBotApi.reply_message(
  70. event.reply_token, linebotModels.AudioSendMessage(
  71. original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
  72. #@app.route("/callback", methods=['POST','GET'])
  73. def callback():
  74. # get X-Line-Signature header value
  75. signature = request.headers['X-Line-Signature']
  76. # get request body as text
  77. body = request.get_data(as_text=True)
  78. app.logger.info("Request body: " + body)
  79. print("Request body: " + body)
  80. # handle webhook body
  81. try:
  82. handler.handle(body, signature)
  83. except InvalidSignatureError:
  84. print("Invalid signature. Please check your channel access token/channel secret.")
  85. abort(400)
  86. return 'OK',200
  87. @app.route('/', methods=['GET'])
  88. def serve():
  89. return "Hello world", 200
  90. @handler.add(MessageEvent, message=TextMessage)
  91. def handle_message(event):
  92. print(event.message.text)
  93. if event.message.text=='業績':
  94. line_bot_api.reply_message(
  95. event.reply_token,
  96. TextSendMessage(text='您本月營收為9,287元'))
  97. if event.message.text=='新貨':
  98. line_bot_api.reply_message(
  99. event.reply_token,
  100. TextSendMessage(text='寶貝的毛巾該換囉! \
  101. 小朋友開學必備!家中必備! \
  102. 這款小方巾,不論是洗臉、嬰兒擦口水 \
  103. 或是帶去學校當手帕都很適合 '))
  104. if event.message.text=='防蚊':
  105. url='https://pic.pimg.tw/youwin721/1593748433-4270703895-g.png'
  106. line_bot_api.reply_message(
  107. event.reply_token,
  108. ImageSendMessage(url,url))
  109. if event.message.text=='華 地址':
  110. line_bot_api.reply_message(
  111. event.reply_token,
  112. TextSendMessage(text='林曉華的收件地址是: 110台北市信義區嘉興街235號'))
  113. if event.message.text=='AA':
  114. confirm_template_message = TemplateSendMessage(
  115. alt_text='Confirm template',
  116. template=ConfirmTemplate(
  117. text='Are you sure?',
  118. actions=[
  119. PostbackAction(
  120. label='postback',
  121. display_text='postback text',
  122. data='action=buy&itemid=1'
  123. ),
  124. MessageAction(
  125. label='message',
  126. text='message text'
  127. )
  128. ]
  129. )
  130. )
  131. line_bot_api.reply_message(event.reply_token,confirm_template_message)
  132. elif event.message.text=='cc':
  133. flex_message = FlexSendMessage(
  134. alt_text='hello',
  135. contents=jsobj
  136. )
  137. line_bot_api.reply_message(event.reply_token,flex_message)
  138. elif event.message.text=='BB':
  139. carousel_template_message = TemplateSendMessage(
  140. alt_text='Carousel template',
  141. template=CarouselTemplate(
  142. columns=[
  143. CarouselColumn(
  144. thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
  145. title='this is menu1',
  146. text='description1',
  147. actions=[
  148. PostbackAction(
  149. label='postback1',
  150. display_text='postback text1',
  151. data='action=buy&itemid=1'
  152. ),
  153. MessageAction(
  154. label='message1',
  155. text='message text1'
  156. ),
  157. URIAction(
  158. label='uri1',
  159. uri='http://example.com/1'
  160. )
  161. ]
  162. ),
  163. CarouselColumn(
  164. thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
  165. title='this is menu2',
  166. text='description2',
  167. actions=[
  168. PostbackAction(
  169. label='postback2',
  170. display_text='postback text2',
  171. data='action=buy&itemid=2'
  172. ),
  173. MessageAction(
  174. label='message2',
  175. text='message text2'
  176. ),
  177. URIAction(
  178. label='uri2',
  179. uri='http://example.com/2'
  180. )
  181. ]
  182. )
  183. ]
  184. )
  185. )
  186. line_bot_api.reply_message(event.reply_token,carousel_template_message)
  187. else:
  188. line_bot_api.reply_message(
  189. event.reply_token,
  190. TextSendMessage(text=event.message.text))
  191. if __name__ == "__main__":
  192. 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'))
  193. # app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))