def create_richmenuid(line_bot_api, rich_menu_name): ''' Create rich menu & Upload rich menu image rich_menu_name: 填入要新增的 圖文選單 新增一個執行一次就好,不然 ID 會有很多個 ''' # Create rich menu and get ID lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict( json.load( open('material/' + rich_menu_name + '/rich_menu.json', 'r', encoding='utf8')))) print('Rich Menu ID:', lineRichMenuId) # record rich menu ID with open('material/' + rich_menu_name + '/rich_menu_id', 'w', encoding='utf8') as f: f.write(lineRichMenuId) # Upload rich menu image setImageResponse = '' with open('material/' + rich_menu_name + '/rich_menu.png', 'rb') as f: setImageResponse = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/png', f) print('Upload Result: ', setImageResponse) return lineRichMenuId
def generate_rich_menu_id(): rich_menu_array = ['rich_menu_b'] from linebot.models import RichMenu for rich_menu_name in rich_menu_array: # 創建菜單,取得menuId lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict( json.load( open("素材/" + rich_menu_name + '/rich_menu.json', 'r', encoding='utf8')))) print("-設定檔上傳結果") print(lineRichMenuId) # id寫入本地端 f = open("素材/" + rich_menu_name + "/rich_menu_id", "w", encoding='utf8') f.write(lineRichMenuId) f.close() # 上傳照片至該id set_image_response = '' with open("素材/" + rich_menu_name + '/rich_menu.jpg', 'rb') as f: set_image_response = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/jpeg', f) print("-圖片上傳結果") print(set_image_response)
def process_follow_event(event): # 讀取並轉換 result_message_array = [] replyJsonPath = "material/Follow/reply.json" result_message_array = detect_json_array_to_new_message_array(replyJsonPath) # 消息發送 line_bot_api.reply_message( event.reply_token, result_message_array ) menuJson = json.load(open('./material/rich_menu/rich_menu.json', 'r', encoding='utf-8')) # 設定圖文選單功能 lineRichMenuId = line_bot_api.create_rich_menu(rich_menu=RichMenu.new_from_json_dict(menuJson)) print(lineRichMenuId) uploadImageFile = open("./material/rich_menu/rich_menu.jpg", 'rb') # 設定圖文選單圖片 setImageResponse = line_bot_api.set_rich_menu_image(lineRichMenuId, 'image/jpeg', uploadImageFile) linkResult = line_bot_api.link_rich_menu_to_user(self_user_id, lineRichMenuId) # 取出消息內User的資料 user_profile = line_bot_api.get_profile(event.source.user_id) # 將用戶資訊存在檔案內 # with open("./users.txt", "a") as myfile: # myfile.write(json.dumps(vars(user_profile), sort_keys=True)) # myfile.write('\n') user_id = str(event.source.user_id) print(user_id) select_userid = ''' SELECT userid from line_bot_db.lineuser; ''' print(type(db.engine.execute(select_userid).fetchall())) try: if user_id not in select_userid: sql2 = f'''INSERT INTO line_bot_db.user_linebot_record(userid) VALUES ("{user_id}");''' db.engine.execute(sql2) sql1 = f'''INSERT INTO line_bot_db.lineuser(userid) VALUES ("{user_id}"); ''' db.engine.execute(sql1) except Exception: pass # 將菜單綁定在用戶身上 line_bot_api.link_rich_menu_to_user(user_id, lineRichMenuId)
def run(): rich_menu_array = [ 'rich_menu_main', 'rich_menu_button_1', 'rich_menu_button_2', 'rich_menu_control', 'rich_menu_control_1' ] for i in range(1, 13): c = 'rich_menu_{}'.format(i) rich_menu_array.append(c) for rich_menu_name in rich_menu_array: lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict( json.load( open('image_trpg_elevator/rich_menu/{}/{}.json'.format( rich_menu_name, rich_menu_name), 'r', encoding='utf-8')))) print('設定上傳結果') print(lineRichMenuId) f = open('image_trpg_elevator/rich_menu/{}/rich_menu_id'.format( rich_menu_name), 'w', encoding='utf-8') f.write(lineRichMenuId) f.close() with open( 'image_trpg_elevator/rich_menu/{}/{}.jpg'.format( rich_menu_name, rich_menu_name), 'rb') as f: set_image_response = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/jpeg', f) print('圖片上傳結果') print(set_image_response) # run() # rich_menu_list = line_bot_api.get_rich_menu_list() # for rich_menu in rich_menu_list: # line_bot_api.delete_rich_menu(rich_menu.rich_menu_id)
"x": 1763, "y": 848, "width": 545, "height": 520 }, "action": { "type": "postback", "data": '{"kind":"menu","level":"%d","choice":"%d","item":"%d"}' % (0, 6, 0) } }] } rich_menu_list = line_bot_api.get_rich_menu_list() if rich_menu_list: print('rich_menu_list', rich_menu_list) line_bot_api.delete_rich_menu(line_bot_api.get_default_rich_menu()) else: myRichMenu_Id = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict(rich_menu_initial)) print('myRichMenu_Id:', myRichMenu_Id) uploadImageFile = open('D:/rich_menu_forallusers.jpg', 'rb') setImageResponse = line_bot_api.set_rich_menu_image( myRichMenu_Id, 'image/jpeg', uploadImageFile) if setImageResponse: print('setImageResponse:', setImageResponse) setImageResponse = line_bot_api.set_default_rich_menu(myRichMenu_Id) default_rich_menu = line_bot_api.get_default_rich_menu() print('default_rich_menu', default_rich_menu)
if os.path.isdir(rich_menu_path): anything = os.listdir(rich_menu_path) file_path = '' jpg_path = '' for file in anything: if file == 'rich_menu.json': file_path = os.path.join(rich_menu_path, file) elif file == 'rich_menu.jpg': jpg_path = os.path.join(rich_menu_path, file) with open(file_path, 'r') as f: rich_menu_json = json.loads(f.read()) # 創建菜單,取得menuId lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict(rich_menu_json)) print('設定檔上傳結果') print(lineRichMenuId) #id寫入指定資料夾端 id_folder = os.path.join(rich_menu_path, 'rich_menu_id.txt') with open(id_folder, 'w') as rich_id: rich_id.write(lineRichMenuId) # 上傳照片至該menu-id with open(jpg_path, 'rb') as f: set_image_response = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/jpeg', f) print("-圖片上傳結果") print(set_image_response)
line_bot_api = LineBotApi(channel_access_token) # 讀取選單 json、jpg,json 利用 Line bot Designer 設計出來 json_path_list = glob.glob(r'.\menu\*.json') jpg_path_list = glob.glob(r'.\menu\*.jpg') # 檔案上傳順序確認 print(json_path_list) print(jpg_path_list) # 依序上傳 for index in range(len(json_path_list)): print(f'上傳第 {index + 1} 組') # 創建菜單,取得 menuId try: lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict( json.load(open(json_path_list[index], 'r', encoding='utf8')))) print(lineRichMenuId) except: print('建立 json 失敗') # 上傳照片至該id try: with open(jpg_path_list[index], 'rb') as f: set_image_response = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/jpeg', f) except: print('上傳失敗') print('上傳完成')
''' rich_menu_array = ['rich_menu_0', 'rich_menu_1', 'rich_menu_2', 'rich_menu_3'] # 載入安全設定檔 secretFileContentJson = json.load( open("./line_secret_key", 'r', encoding='utf8')) line_bot_api = LineBotApi(secretFileContentJson.get("channel_access_token")) from linebot.models import RichMenu for rich_menu_name in rich_menu_array: # 創建菜單,取得menuId lineRichMenuId = line_bot_api.create_rich_menu( rich_menu=RichMenu.new_from_json_dict( json.load( open("素材/" + rich_menu_name + '/rich_menu.json', 'r', encoding='utf8')))) print("-設定檔上傳結果") print(lineRichMenuId) # id寫入本地端 f = open("素材/" + rich_menu_name + "/rich_menu_id", "w", encoding='utf8') f.write(lineRichMenuId) f.close() # 上傳照片至該id set_image_response = '' with open("素材/" + rich_menu_name + '/rich_menu.jpg', 'rb') as f: set_image_response = line_bot_api.set_rich_menu_image( lineRichMenuId, 'image/jpeg', f)
"type": "postback", "text": "準備進貢", "data": "menu=準備進貢" } } ] } ''' ''' 載入前面的圖文選單設定, 並要求line_bot_api將圖文選單上傳至Line ''' menuJson=json.loads(menuRawData) lineRichMenuId = line_bot_api.create_rich_menu(RichMenu.new_from_json_dict(menuJson)) print(lineRichMenuId) ''' 將先前準備的菜單照片,以Post消息寄發給Line 載入照片 要求line_bot_api,將圖片傳到先前的圖文選單id ''' uploadImageFile=open("./素材/images/richmenu.jpg",'rb') setImageResponse = line_bot_api.set_rich_menu_image(lineRichMenuId,'image/jpeg',uploadImageFile) print(setImageResponse) '''