def weather(data, location): lat = location["lat"] lon = location["long"] weather_a = requests.get( "http://api.openweathermap.org/data/2.5/weather?lang=zh_tw&units=metric&lat={}&lon={}&appid=9ec4e2aec0d50139161b0722fc392f15" .format(lat, lon)) weather_bot = json.loads(weather_a.text) for weather_bot2 in weather_bot["weather"]: weather_status = weather_bot2["description"] weather_icon = weather_bot2['icon'] weather_temp = weather_bot["main"]["temp"] weather_text = '出門要注意安全掰掰' if weather_status.find('雲') != -1: weather_text = '出門要注意安全掰掰' elif weather_status.find('晴') != -1: weather_text = '請注意防曬再出門,以免紫外線傷害皮膚' elif weather_status.find('雨') != -1: weather_text = '請攜帶雨具出門,注意別淋濕感冒了' elif weather_status.find('雪') != -1: weather_text = '出門要注意保暖喔' writepic(data, "天氣:{},氣溫:{}".format(weather_status, round(weather_temp)), "http://openweathermap.org/img/w/{}.png".format(weather_icon)) write(data, weather_text)
def show_store_detail(session, index, with_keyboard = True): replyKeyboard = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text="⤴️上一頁",callback_data="return_to_stores")], [InlineKeyboardButton(text="👌OK",callback_data="OK" + str(index))] ]) store = session["result_list"][index] return write(data = None, msg = "以下為此店家的相關資訊:\n" "🏠 店名:" + store['name'] + "\n" "📞 電話:" + store['tel'] + "\n" "🚲 距離:" + str(store['dis']) + "m\n" "📝 地址:" + store['add'] + "\n" "📊 評等:" + "⭐" * store["rating_rounded"] + store["rating_string"] + "\n" "⏰ 營業:" + store["open"] + "\n" ,keyboard = replyKeyboard if with_keyboard else None,session = session)
def show_type_selection(data = None,session = None): """ Show a message with keyboard to choose the desired shop type. data (dict): Data dict generated by read functions in bot.py session (dict): A dict representin the session or None if no session has been created return: message (dict, telegram message object https://core.telegram.org/bots/api#message) """ replyKeyboard = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text="🍞 麵包店",callback_data="type0")], [InlineKeyboardButton(text="☕️ 咖啡廳",callback_data="type1")], [InlineKeyboardButton(text="🍛 餐廳",callback_data="type2")], [InlineKeyboardButton(text="🍺 酒吧",callback_data="type3")], ]) return write(data=data,msg="要選擇什麼類型的食物呢?",keyboard=replyKeyboard,session=session)
def show_stores(session): """ Show a message with keyboard to choose the desire store. session (dict): A dict representin the session return: message (dict, telegram message object https://core.telegram.org/bots/api#message) """ inline_keyboard = [] for i,restaurant in enumerate(session["result_list"]): inline_keyboard.append([InlineKeyboardButton(text="✔️" + restaurant['name'] + "\n (" + str(restaurant['dis']) + "m)", callback_data="store" + str(i))]) inline_keyboard.append([InlineKeyboardButton(text="⤴️上一頁",callback_data="return_to_type")]) replyKeyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard) return write(data = None, msg = "以下為距離您最近的 {} 家店家:".format(len(session["result_list"])), keyboard = replyKeyboard,session = session)
def run(self): while True: try: data = read() if not self.running: return if data == None or data["type"] == "error": continue print(data) if data["type"] == "text": if data["text"].startswith("/"): write(data, "請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:") else: location = get_location(data["text"]) if location == None: write(data, "抱歉,您所提供的地點資料不足") else: self.start_session(data, location) elif data["type"] == "location": location = {"lat": data["lat"], "long": data["long"]} self.start_session(data, location) elif data["type"] == "callback": answer_callback(data) try: sess = self.load_session( self.session_id_from_message(data["message"])) except FileNotFoundError: write( data, "很抱歉,此按鈕已經失效。\n請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:" ) continue if data["data"][0:4] == "type": #write(data=None,msg = "處理中...",keyboard = None,session = sess) sess["place_type"] = [ "bakery", "cafe", "restaurant", "bar" ][int(data["data"][4])] sess["result_list"] = get_search( sess["location"]["lat"], sess["location"]["long"], sess["place_type"]) if sess["result_list"] is None: write(data, "抱歉,無法找到符合條件的搜尋結果") continue else: gui.show_stores(sess) self.save_session(sess) elif data["data"] == "return_to_type": gui.show_type_selection(session=sess) elif data["data"] == "return_to_stores": gui.show_stores(session=sess) elif data["data"][:2] == "OK": sid = int(data["data"][2]) if sid >= len(sess["result_list"]): write( data, "很抱歉,此按鈕已經失效。\n請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:" ) continue store = sess["result_list"][sid] gui.show_store_detail(sess, sid, with_keyboard=False) write_location(data, store['name'], store['lat'], store['long'], store['add']) write(data, "以下是現在的天氣狀況:") location = {"long": store["long"], "lat": store["lat"]} weather(data, location) write(data, "祝你有個美好的用餐時光~") elif data["data"][:5] == "store": #write(data=None,msg = "處理中...",keyboard = None,session = sess) sid = int(data["data"][5]) if sid >= len(sess["result_list"]): write( data, "很抱歉,此按鈕已經失效。\n請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:" ) else: detail = get_detail(sess["result_list"][sid]["id"]) sess["result_list"][sid].update(detail) gui.show_store_detail(sess, sid) self.save_session(sess) else: write( data, "很抱歉,此按鈕已經失效。\n請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:" ) else: write(data, "請直接輸入您所在的位置或是傳送手機的定位資訊\n我將會幫你搜尋附近的食物:") except: Logger.log(Logger.ERROR, "%s", str(sys.exc_info()))