def fetch_and_send_photo(chat_id, hash_tag, text, replies): """ Sends a photo from instagram to user by specified hashtag :param chat_id: User's telegram chat id :param hash_tag: Hashtag to fetch photo from instagram :param text: Additional text to send after sending photo :param replies: Telegram keyboard replies :return: """ logging.warning(f"Fetching photo with hashtag: {hash_tag}") try: # Getting url for the instagram photo photo_url = random.choice(get_image_urls_by_hash_tag(hash_tag)) # Send photo send_photo(chat_id, photo_url) # Send text message after photo send_text(chat_id, text, reply_keyboard=get_reply_keyboard(replies)) # Get or create user record in DB user = User.get(chat_id=chat_id) or User(chat_id=chat_id) # Update user record with the last search word user.last_search_word = hash_tag except: traceback.print_exc() # Something went wrong with the instagram response_text = "👺 Instagram sucks. I will show you some gif on a meantime." # Fetching url from giphy gif_url = giphy_client.get_random_gif_url(text) # Sending giphy url send_text(chat_id, f"{response_text}\n{gif_url}")
def send_embrace_to_user(user: User): """ Sends embrace giphy to user with any subscription :param user: User to send """ if user.subscription_word: send_text(user.chat_id, f"Hello there 😜!\nIt is time for a virtual embrace:") embrace_url = GiphyApiClient().get_random_gif_url('embrace') resp = send_text(user.chat_id, embrace_url) print(resp)
def process(self): user = User.get(chat_id=self.request.chat_id) # Get subscription work as a last search work user.subscription_word = user.last_search_word text = f"Cool 😎.\n" text += f"Now you will receive top **{user.subscription_word}** photos every day 👌.\n" text += f"Stay in touch!" # Send confirmation message send_text(self.request.chat_id, text)
def process(self): """ Sends welcome text to user """ welcome_text = "Welcome 🙌" welcome_text += f", {self.request.first_name}.\n" text = f"My name is {os.environ.get('NAME')} and I really know where to get the best photos.\n" \ f"Type me any word and I will return a related photo.\n" \ f"For example, try to type **sunset**." send_text(self.request.chat_id, text=welcome_text + text, reply_keyboard=get_reply_keyboard(["sunset"]))
def send_photo_to_user(user: User): """ Sends photo to user related to the subscription :param user: User to send """ if user.subscription_word: send_text( user.chat_id, f"Hello there 😜!\nIt is time to receive a daily **{user.subscription_word}** photo." ) faces_on_image = None # Checking if there are faces on image while faces_on_image != False: photo_url = random.choice( get_image_urls_by_hash_tag(user.subscription_word)) faces_on_image = get_count_of_faces(photo_url) != 0 send_photo(user.chat_id, photo_url)
def message(): request = Request(flask_request) # Processing message here try: # Hello message if request.text == '/start': HelloController(request).process() # Subscribe message elif request.text.lower() == "subscribe": SubscriptionsController(request).process() # One more message elif request.text.lower().startswith("one more"): OneMorePhotoController(request).process() # Sends photos message else: SendPhotosController(request).process() except: traceback.print_exc() # If something went totally wrong, send callback message send_text(request.chat_id, "Sorry, I'm broken!") return "OK"