1

我有发送照片的基本代码,我似乎理解选择随机照片的概念。我的问题是我应该在这段代码中的哪个位置放置随机选择代码?

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)   


4

0 回答 0