2

I would like to select one random update.message from that list when the music funtion is called. I assume I should store all the links outside of the update.message so it doesnt send all 3 at the call of music

def music(bot, update):

    update.message.reply_text("https://www.youtube.com/watch?v=XErrOJGzKv8")
    update.message.reply_text("https://www.youtube.com/watch?v=hHW1oY26kxQ")
    update.message.reply_text("https://www.youtube.com/watch?v=RmHg6BP5D8g")
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80

4 Answers4

2

use this

import random
foo=['link1','link2','link3']
random_link=random.choice(foo)
#call your function here using random_link

refer this question too How to randomly select an item from a list?

Nuwan
  • 475
  • 2
  • 5
  • 14
0

random is great at choosing something random.

import random
playlist = ['https://www.youtube.com/watch?v=XErrOJGzKv8','https://www.youtube.com/watch?v=hHW1oY26kxQ', 'https://www.youtube.com/watch?v=RmHg6BP5D8g']

def music(bot, update):
    update.message.reply_text(random.choice(playlist)) # random.choice() chooses a random item from a list
boop
  • 36
  • 5
0

Use random.choice(seq)

import random

def music (bot, update):

    list_of_urls = ["www.your-urls.com", "www.your-urls2.com"]
    random_url = random.choice(list_of_urls)

    update.message.reply_text(random_url)
Wong Siwei
  • 115
  • 2
  • 6
0

First step: store your values in a list.

Second step: just use random.choice()

import random

my_links = [
    "https://www.youtube.com/watch?v=XErrOJGzKv8",
    "https://www.youtube.com/watch?v=hHW1oY26kxQ",
    "https://www.youtube.com/watch?v=RmHg6BP5D8g"
]


def music(bot, update):
    update.message.reply_text(random.choice(my_links))
Felipe
  • 130
  • 3