├── README.md ├── Procfile ├── requirements.txt ├── static └── tmp │ └── tmp.jpg ├── __pycache__ ├── new.cpython-37.pyc ├── Function.cpython-37.pyc └── message.cpython-37.pyc ├── new.py ├── app.py ├── Function.py └── message.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: python app.py -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | line-bot-sdk 2 | flask 3 | gunicorn 4 | -------------------------------------------------------------------------------- /static/tmp/tmp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maso0310/linebot/HEAD/static/tmp/tmp.jpg -------------------------------------------------------------------------------- /__pycache__/new.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maso0310/linebot/HEAD/__pycache__/new.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/Function.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maso0310/linebot/HEAD/__pycache__/Function.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/message.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maso0310/linebot/HEAD/__pycache__/message.cpython-37.pyc -------------------------------------------------------------------------------- /new.py: -------------------------------------------------------------------------------- 1 | #這些是LINE官方開放的套件組合透過import來套用這個檔案上 2 | from linebot import (LineBotApi, WebhookHandler) 3 | from linebot.exceptions import (InvalidSignatureError) 4 | from linebot.models import * 5 | 6 | def test(): 7 | message = TemplateSendMessage( 8 | alt_text='圖片旋轉木馬', 9 | template=ImageCarouselTemplate( 10 | columns=[ 11 | ImageCarouselColumn( 12 | image_url="https://i.imgur.com/uKYgfVs.jpg", 13 | action=URITemplateAction( 14 | label="新鮮水果", 15 | uri="http://img.juimg.com/tuku/yulantu/110709/222-110F91G31375.jpg" 16 | ) 17 | ), 18 | ImageCarouselColumn( 19 | image_url="https://i.imgur.com/QOcAvjt.jpg", 20 | action=URITemplateAction( 21 | label="新鮮蔬菜", 22 | uri="https://cdn.101mediaimage.com/img/file/1410464751urhp5.jpg" 23 | ) 24 | ), 25 | ImageCarouselColumn( 26 | image_url="https://i.imgur.com/Np7eFyj.jpg", 27 | action=URITemplateAction( 28 | label="可愛狗狗", 29 | uri="http://imgm.cnmo-img.com.cn/appimg/screenpic/big/674/673928.JPG" 30 | ) 31 | ), 32 | ImageCarouselColumn( 33 | image_url="https://i.imgur.com/QRIa5Dz.jpg", 34 | action=URITemplateAction( 35 | label="可愛貓咪", 36 | uri="https://m-miya.net/wp-content/uploads/2014/07/0-065-1.min_.jpg" 37 | ) 38 | ) 39 | ] 40 | ) 41 | ) 42 | return message -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, abort 2 | 3 | from linebot import ( 4 | LineBotApi, WebhookHandler 5 | ) 6 | from linebot.exceptions import ( 7 | InvalidSignatureError 8 | ) 9 | from linebot.models import * 10 | 11 | 12 | #======這裡是呼叫的檔案內容===== 13 | from message import * 14 | from new import * 15 | from Function import * 16 | #======這裡是呼叫的檔案內容===== 17 | 18 | #======python的函數庫========== 19 | import tempfile, os 20 | import datetime 21 | import time 22 | #======python的函數庫========== 23 | 24 | app = Flask(__name__) 25 | static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp') 26 | # Channel Access Token 27 | line_bot_api = LineBotApi('你的Channel AcessToken') 28 | # Channel Secret 29 | handler = WebhookHandler('你的Channel Secret') 30 | 31 | 32 | # 監聽所有來自 /callback 的 Post Request 33 | @app.route("/callback", methods=['POST']) 34 | def callback(): 35 | # get X-Line-Signature header value 36 | signature = request.headers['X-Line-Signature'] 37 | # get request body as text 38 | body = request.get_data(as_text=True) 39 | app.logger.info("Request body: " + body) 40 | # handle webhook body 41 | try: 42 | handler.handle(body, signature) 43 | except InvalidSignatureError: 44 | abort(400) 45 | return 'OK' 46 | 47 | 48 | # 處理訊息 49 | @handler.add(MessageEvent, message=TextMessage) 50 | def handle_message(event): 51 | msg = event.message.text 52 | if '最新合作廠商' in msg: 53 | message = imagemap_message() 54 | line_bot_api.reply_message(event.reply_token, message) 55 | elif '最新活動訊息' in msg: 56 | message = buttons_message() 57 | line_bot_api.reply_message(event.reply_token, message) 58 | elif '註冊會員' in msg: 59 | message = Confirm_Template() 60 | line_bot_api.reply_message(event.reply_token, message) 61 | elif '旋轉木馬' in msg: 62 | message = Carousel_Template() 63 | line_bot_api.reply_message(event.reply_token, message) 64 | elif '圖片畫廊' in msg: 65 | message = test() 66 | line_bot_api.reply_message(event.reply_token, message) 67 | elif '功能列表' in msg: 68 | message = function_list() 69 | line_bot_api.reply_message(event.reply_token, message) 70 | else: 71 | message = TextSendMessage(text=msg) 72 | line_bot_api.reply_message(event.reply_token, message) 73 | 74 | @handler.add(PostbackEvent) 75 | def handle_message(event): 76 | print(event.postback.data) 77 | 78 | 79 | @handler.add(MemberJoinedEvent) 80 | def welcome(event): 81 | uid = event.joined.members[0].user_id 82 | gid = event.source.group_id 83 | profile = line_bot_api.get_group_member_profile(gid, uid) 84 | name = profile.display_name 85 | message = TextSendMessage(text=f'{name}歡迎加入') 86 | line_bot_api.reply_message(event.reply_token, message) 87 | 88 | 89 | import os 90 | if __name__ == "__main__": 91 | port = int(os.environ.get('PORT', 5000)) 92 | app.run(host='0.0.0.0', port=port) 93 | -------------------------------------------------------------------------------- /Function.py: -------------------------------------------------------------------------------- 1 | #這個檔案的作用是:建立功能列表 2 | 3 | #===============這些是LINE提供的功能套組,先用import叫出來============= 4 | from linebot import (LineBotApi, WebhookHandler) 5 | from linebot.exceptions import (InvalidSignatureError) 6 | from linebot.models import * 7 | #===============LINEAPI============================================= 8 | 9 | #以下是本檔案的內容本文 10 | 11 | #1.建立旋轉木馬訊息,名為function_list(未來可以叫出此函數來使用) 12 | #function_list的括號內是設定此函數呼叫時需要給函數的參數有哪些 13 | 14 | def function_list(): 15 | message = TemplateSendMessage( 16 | alt_text='功能列表', 17 | template=CarouselTemplate( 18 | columns=[ 19 | CarouselColumn( 20 | thumbnail_image_url='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQkl5qgGtBxZbBu921rynn7HN7C7JaD_Hbi5cMMV5gEgQu2mE-rIw', 21 | title='Maso萬事屋百貨', 22 | text='百萬種商品一站購足', 23 | actions=[ 24 | MessageTemplateAction( 25 | label='關於Maso百貨', 26 | text='Maso萬事屋百貨是什麼呢?' 27 | ), 28 | URITemplateAction( 29 | label='點我逛百貨', 30 | uri='https://tw.shop.com/maso0310' 31 | ) 32 | ] 33 | ), 34 | CarouselColumn( 35 | thumbnail_image_url='https://www.youtaker.com/video2015/promo/images/promo-vip.png', 36 | title='註冊成為會員', 37 | text='免費獲得會員好康!', 38 | actions=[ 39 | MessageTemplateAction( 40 | label='會員優惠資訊', 41 | text='我想瞭解註冊會員的好處是什麼' 42 | ), 43 | URITemplateAction( 44 | label='點我註冊會員', 45 | uri='https://tw.shop.com/nbts/create-myaccount.xhtml?returnurl=https%3A%2F%2Ftw.shop.com%2F' 46 | ) 47 | ] 48 | ), 49 | CarouselColumn( 50 | thumbnail_image_url='https://img.shop.com/Image/Images/11module/MABrands/opc3Chews_usa_32979_LogoTreatment_200x75.svg', 51 | title='獨家商品', 52 | text='百種優質獨家商品', 53 | actions=[ 54 | MessageTemplateAction( 55 | label='點我看產品目錄', 56 | text='獨家商品有哪些?' 57 | ), 58 | URITemplateAction( 59 | label='購買獨家品牌', 60 | uri='https://tw.shop.com/info/our-brands' 61 | ) 62 | ] 63 | ), 64 | CarouselColumn( 65 | thumbnail_image_url='https://img.shop.com/Image/featuredhotdeal/GOMAJI1551245496503.jpg', 66 | title='優惠資訊', 67 | text='隨時更新最新優惠', 68 | actions=[ 69 | MessageTemplateAction( 70 | label='抽一個優惠', 71 | text='抽優惠資訊' 72 | ), 73 | URITemplateAction( 74 | label='近期優惠資訊', 75 | uri='https://tw.shop.com/hot-deals' 76 | ) 77 | ] 78 | ), 79 | CarouselColumn( 80 | thumbnail_image_url='https://img.shop.com/Image/featuredhotdeal/Carrefour1551245288925.jpg', 81 | title='最新消息', 82 | text='最新活動訊息', 83 | actions=[ 84 | MessageTemplateAction( 85 | label='點我看最新消息', 86 | text='我想瞭解最新活動' 87 | ), 88 | URITemplateAction( 89 | label='活動資訊頁面', 90 | uri='https://tw.shop.com/hot-deals' 91 | ) 92 | ] 93 | ), 94 | CarouselColumn( 95 | thumbnail_image_url='http://img.technews.tw/wp-content/uploads/2014/05/TechNews-624x482.jpg', 96 | title='每日新知', 97 | text='定期更新相關資訊', 98 | actions=[ 99 | MessageTemplateAction( 100 | label='點我看每日新知', 101 | text='抽一則每日新知' 102 | ), 103 | URITemplateAction( 104 | label='更多更新內容', 105 | uri='https://www.youtube.com/channel/UCpzVAEwEs9AwT2uAOZuxaRQ?view_as=subscriber' 106 | ) 107 | ] 108 | ), 109 | CarouselColumn( 110 | thumbnail_image_url='https://www.wecooperation.com/makemoney/%E7%9F%A5%E5%90%8D%E5%A4%A5%E4%BC%B4%E5%95%86%E5%BA%97.png', 111 | title='好店分享', 112 | text='優質商品介紹與分享', 113 | actions=[ 114 | MessageTemplateAction( 115 | label='夥伴商店推薦', 116 | text='抽一家夥伴商店' 117 | ), 118 | URITemplateAction( 119 | label='查詢夥伴商店', 120 | uri='https://tw.shop.com/stores-a-z' 121 | ) 122 | ] 123 | ), 124 | CarouselColumn( 125 | thumbnail_image_url='https://img.shop.com/Image/Images/landingPages/ps-recruit/twn-ps-recruit-header.jpg', 126 | title='招商說明', 127 | text='與Shop.com合作', 128 | actions=[ 129 | MessageTemplateAction( 130 | label='招商資訊', 131 | text='如何成為夥伴商店' 132 | ), 133 | URITemplateAction( 134 | label='招商說明報名頁面', 135 | uri='https://tw.shop.com/ps_recruit_intro-v.xhtml?tkr=180530162209' 136 | ) 137 | ] 138 | ), 139 | CarouselColumn( 140 | thumbnail_image_url='https://images.marketamerica.com/site/br/images/logos/awards/torch-award-ethics-2018.jpg', 141 | title='微型創業資訊', 142 | text='加入網路微型創業趨勢', 143 | actions=[ 144 | MessageTemplateAction( 145 | label='瞭解更多', 146 | text='什麼是微型創業資訊' 147 | ), 148 | URITemplateAction( 149 | label='公司簡介', 150 | uri='https://www.marketamerica.com/?localeCode=zh-Hant&redirect=true' 151 | ) 152 | ] 153 | ), 154 | CarouselColumn( 155 | thumbnail_image_url='https://scontent-sjc3-1.xx.fbcdn.net/v/t1.0-1/p320x320/50934385_2553136691368417_7766092240367124480_n.jpg?_nc_cat=109&_nc_ht=scontent-sjc3-1.xx&oh=c144a6b45450781ccaf258beb40bc53e&oe=5D228BF1', 156 | title='聯繫Maso本人', 157 | text='直接聯繫Maso', 158 | actions=[ 159 | MessageTemplateAction( 160 | label='誰是Maso?', 161 | text='Maso是誰?想認識' 162 | ), 163 | URITemplateAction( 164 | label='加我的LINE', 165 | uri='https://line.me/ti/p/KeRocPY6PP' 166 | ) 167 | ] 168 | ) 169 | ] 170 | ) 171 | ) 172 | return message -------------------------------------------------------------------------------- /message.py: -------------------------------------------------------------------------------- 1 | #這些是LINE官方開放的套件組合透過import來套用這個檔案上 2 | from linebot import (LineBotApi, WebhookHandler) 3 | from linebot.exceptions import (InvalidSignatureError) 4 | from linebot.models import * 5 | 6 | #ImagemapSendMessage(組圖訊息) 7 | def imagemap_message(): 8 | message = ImagemapSendMessage( 9 | base_url="https://i.imgur.com/BfTFVDN.jpg", 10 | alt_text='最新的合作廠商有誰呢?', 11 | base_size=BaseSize(height=2000, width=2000), 12 | actions=[ 13 | URIImagemapAction( 14 | #家樂福 15 | link_uri="https://tw.shop.com/search/%E5%AE%B6%E6%A8%82%E7%A6%8F", 16 | area=ImagemapArea( 17 | x=0, y=0, width=1000, height=1000 18 | ) 19 | ), 20 | URIImagemapAction( 21 | #生活市集 22 | link_uri="https://tw.shop.com/search/%E7%94%9F%E6%B4%BB%E5%B8%82%E9%9B%86", 23 | area=ImagemapArea( 24 | x=1000, y=0, width=1000, height=1000 25 | ) 26 | ), 27 | URIImagemapAction( 28 | #阿瘦皮鞋 29 | link_uri="https://tw.shop.com/search/%E9%98%BF%E7%98%A6%E7%9A%AE%E9%9E%8B", 30 | area=ImagemapArea( 31 | x=0, y=1000, width=1000, height=1000 32 | ) 33 | ), 34 | URIImagemapAction( 35 | #塔吉特千層蛋糕 36 | link_uri="https://tw.shop.com/search/%E5%A1%94%E5%90%89%E7%89%B9", 37 | area=ImagemapArea( 38 | x=1000, y=1000, width=1000, height=500 39 | ) 40 | ), 41 | URIImagemapAction( 42 | #亞尼克生乳捲 43 | link_uri="https://tw.shop.com/search/%E4%BA%9E%E5%B0%BC%E5%85%8B", 44 | area=ImagemapArea( 45 | x=1000, y=1500, width=1000, height=500 46 | ) 47 | ) 48 | ] 49 | ) 50 | return message 51 | 52 | #TemplateSendMessage - ButtonsTemplate (按鈕介面訊息) 53 | def buttons_message(): 54 | message = TemplateSendMessage( 55 | alt_text='好消息來囉~', 56 | template=ButtonsTemplate( 57 | thumbnail_image_url="https://pic2.zhimg.com/v2-de4b8114e8408d5265503c8b41f59f85_b.jpg", 58 | title="是否要進行抽獎活動?", 59 | text="輸入生日後即獲得抽獎機會", 60 | actions=[ 61 | DatetimePickerTemplateAction( 62 | label="請選擇生日", 63 | data="input_birthday", 64 | mode='date', 65 | initial='1990-01-01', 66 | max='2019-03-10', 67 | min='1930-01-01' 68 | ), 69 | MessageTemplateAction( 70 | label="看抽獎品項", 71 | text="有哪些抽獎品項呢?" 72 | ), 73 | URITemplateAction( 74 | label="免費註冊享回饋", 75 | uri="https://tw.shop.com/nbts/create-myaccount.xhtml?returnurl=https%3A%2F%2Ftw.shop.com%2F" 76 | ) 77 | ] 78 | ) 79 | ) 80 | return message 81 | 82 | #TemplateSendMessage - ConfirmTemplate(確認介面訊息) 83 | def Confirm_Template(): 84 | 85 | message = TemplateSendMessage( 86 | alt_text='是否註冊成為會員?', 87 | template=ConfirmTemplate( 88 | text="是否註冊成為會員?", 89 | actions=[ 90 | PostbackTemplateAction( 91 | label="馬上註冊", 92 | text="現在、立刻、馬上", 93 | data="會員註冊" 94 | ), 95 | MessageTemplateAction( 96 | label="查詢其他功能", 97 | text="查詢其他功能" 98 | ) 99 | ] 100 | ) 101 | ) 102 | return message 103 | 104 | #旋轉木馬按鈕訊息介面 105 | 106 | def Carousel_Template(): 107 | message = TemplateSendMessage( 108 | alt_text='一則旋轉木馬按鈕訊息', 109 | template=CarouselTemplate( 110 | columns=[ 111 | CarouselColumn( 112 | thumbnail_image_url='https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Number_1_in_green_rounded_square.svg/200px-Number_1_in_green_rounded_square.svg.png', 113 | title='這是第一塊模板', 114 | text='一個模板可以有三個按鈕', 115 | actions=[ 116 | PostbackTemplateAction( 117 | label='回傳一個訊息', 118 | data='將這個訊息偷偷回傳給機器人' 119 | ), 120 | MessageTemplateAction( 121 | label='用戶發送訊息', 122 | text='我知道這是1' 123 | ), 124 | URITemplateAction( 125 | label='進入1的網頁', 126 | uri='https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Number_1_in_green_rounded_square.svg/200px-Number_1_in_green_rounded_square.svg.png' 127 | ) 128 | ] 129 | ), 130 | CarouselColumn( 131 | thumbnail_image_url='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRuo7n2_HNSFuT3T7Z9PUZmn1SDM6G6-iXfRC3FxdGTj7X1Wr0RzA', 132 | title='這是第二塊模板', 133 | text='副標題可以自己改', 134 | actions=[ 135 | PostbackTemplateAction( 136 | label='回傳一個訊息', 137 | data='這是ID=2' 138 | ), 139 | MessageTemplateAction( 140 | label='用戶發送訊息', 141 | text='我知道這是2' 142 | ), 143 | URITemplateAction( 144 | label='進入2的網頁', 145 | uri='https://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/Number_2_in_light_blue_rounded_square.svg/200px-Number_2_in_light_blue_rounded_square.svg.png' 146 | ) 147 | ] 148 | ), 149 | CarouselColumn( 150 | thumbnail_image_url='https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Number_3_in_yellow_rounded_square.svg/200px-Number_3_in_yellow_rounded_square.svg.png', 151 | title='這是第三個模塊', 152 | text='最多可以放十個', 153 | actions=[ 154 | PostbackTemplateAction( 155 | label='回傳一個訊息', 156 | data='這是ID=3' 157 | ), 158 | MessageTemplateAction( 159 | label='用戶發送訊息', 160 | text='我知道這是3' 161 | ), 162 | URITemplateAction( 163 | label='uri2', 164 | uri='https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Number_3_in_yellow_rounded_square.svg/200px-Number_3_in_yellow_rounded_square.svg.png' 165 | ) 166 | ] 167 | ) 168 | ] 169 | ) 170 | ) 171 | return message 172 | 173 | #TemplateSendMessage - ImageCarouselTemplate(圖片旋轉木馬) 174 | def image_carousel_message1(): 175 | message = TemplateSendMessage( 176 | alt_text='圖片旋轉木馬', 177 | template=ImageCarouselTemplate( 178 | columns=[ 179 | ImageCarouselColumn( 180 | image_url="https://i.imgur.com/uKYgfVs.jpg", 181 | action=URITemplateAction( 182 | label="新鮮水果", 183 | uri="http://img.juimg.com/tuku/yulantu/110709/222-110F91G31375.jpg" 184 | ) 185 | ), 186 | ImageCarouselColumn( 187 | image_url="https://i.imgur.com/QOcAvjt.jpg", 188 | action=URITemplateAction( 189 | label="新鮮蔬菜", 190 | uri="https://cdn.101mediaimage.com/img/file/1410464751urhp5.jpg" 191 | ) 192 | ), 193 | ImageCarouselColumn( 194 | image_url="https://i.imgur.com/Np7eFyj.jpg", 195 | action=URITemplateAction( 196 | label="可愛狗狗", 197 | uri="http://imgm.cnmo-img.com.cn/appimg/screenpic/big/674/673928.JPG" 198 | ) 199 | ), 200 | ImageCarouselColumn( 201 | image_url="https://i.imgur.com/QRIa5Dz.jpg", 202 | action=URITemplateAction( 203 | label="可愛貓咪", 204 | uri="https://m-miya.net/wp-content/uploads/2014/07/0-065-1.min_.jpg" 205 | ) 206 | ) 207 | ] 208 | ) 209 | ) 210 | return message 211 | 212 | #關於LINEBOT聊天內容範例 --------------------------------------------------------------------------------