1

I have the basic code for sending a photo and i seem to understand the concept of choosing a random one. My question is where in this code should i put the random choice code?

import sys
import time
import os
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
import random

def on_chat_message(msg):
    global chat_id
    
    content_type, chat_type, chat_id = telepot.glance(msg)
#creating buttons
    if content_type == 'text':
        if msg['text'] == '/start':
           bot.sendMessage(chat_id, 'Welcome to randomImgBot\nOn each click i will display you a random image of your choice\n     Created by JonSnow 2021',reply_markup = InlineKeyboardMarkup(inline_keyboard=[
                                    [InlineKeyboardButton(text="PGP",callback_data='a')]]))
def on_callback_query(msg):
    global chat_id
   
   
    query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
    print('Callback Query:', query_id, from_id, query_data)

   
    

                                  
    if query_data == 'a':
        bot.sendPhoto(chat_id, photo=open('lolly.png'))    
           
                                    
    

bot = telepot.Bot('')
MessageLoop(bot, {'chat': on_chat_message,
                  'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

while 1:
    time.sleep(10)   


ible
  • 55
  • 6
  • 2
    If I understood the question and the source, it would be inserted in `bot.sendPhoto(chat_id, photo=open('lolly.png'))`. You would choose a random photo, instead of `lolly.png`. – Haroldo_OK Mar 03 '21 at 14:36
  • right and how would it look? Could i maybe make it its own function and call on it instead of specifying the path? – ible Mar 03 '21 at 14:42
  • 1
    Yes, you can make a function, if you want. where do you want to take the file list, from? If its from a folder, for example, there exists a Python function to list all the files available on a folder; your could take that list and then pick a random element. – Haroldo_OK Mar 03 '21 at 19:55
  • Thanks for your reply. The file is a local folder on my machine. Would i create the function first and then call back to it in the sendPhoto bit? – ible Mar 04 '21 at 12:29
  • Yes, you could. – Haroldo_OK Mar 04 '21 at 14:28

0 Answers0