My question is the following, I am making a Telegram bot with help to the user in various cases. I would like to capture a piece of the message that it sends me to use it as a variable, that is:
switch($mensaje)
case '/tiempo Madrid':
$ciudad="Madrid";
$horas=FALSE;
getTiempo($chatId,$ciudad,$horas);
break;
In this case I would like not to have to write the city directly in the case, but to make a kind of /tiempo $ciudad
, whose variable I $ciudad
can use to call the function getTiempo()
and thus not have to write all the cities of the world one by one as different cases.
That is, something like this:
switch($mensaje)
case '/tiempo $ciudad':
$horas=FALSE;
getTiempo($chatId,$ciudad,$horas);
break;
Thank you very much for the help.
I don't think you can do the double variable switch, but look at this alternative: - I suggest you separate the command from the message, that way you can get the data separately and do whatever you want with it.
I really don't know of a way to dynamically generate the cases of a switch, but I think maybe this alternative can help you. You simply define an array with all the cities (in your case I imagine they can come from a database), then you go through the array and compare the city of the array with that of the message, once it is found, the if code is executed and the loop stops.
I hope it helps you