Answer the question
In order to leave comments, you need to log in
How to perform correct value substitution in the presence of an 'if' condition?
Hello! I start the bot, I press the button - everything works ok, I press the button a second time - there is a problem that the 'session_id' that I generate is substituted into the request the same as the first time I pressed it. And I need that every time the button is pressed, the random value 'session_id' is substituted, that is, each button press = "new session", tell me, please, how to be ..
import telebot
from telebot import types
import requests
import uuid
bot = telebot.TeleBot("MY_TOKEN")
session_id = uuid.uuid4()
url="MY_LINK"
headers = {'content-type': 'text/xml'}
request1 = f"""<typ:simple>
<typ:attributeName>Session_Id</typ:attributeName>
<typ:value>{session_id}</typ:value>
</typ:simple>"""
request2 = f"""<typ:simple>
<typ:attributeName>Session_Id</typ:attributeName>
<typ:value>{session_id}</typ:value>
</typ:simple>"""
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.send_message(
message.chat.id,
"""Тестовое сообщение""",
reply_markup=keyboard())
def keyboard():
markup = types.ReplyKeyboardMarkup(one_time_keyboard=False, resize_keyboard=True)
markup.add('запрос1')
return markup
@bot.message_handler(content_types=['text'])
def send_text(message):
if message.text.lower() == 'запрос1':
response1 = requests.post(url,data=request1.encode('utf-8'),headers=headers)
answer1 = response1.content.decode('UTF-8')
status_code1 = response1.status_code
bot.send_message(message.chat.id, answer1, status_code1)
if 'Session_Id' in answer1:
response2 = requests.post(url,data=request2.encode('utf-8'),headers=headers)
answer2 = response2.content.decode('UTF-8')
status_code2 = response2.status_code
bot.send_message(message.chat.id, answer2, status_code2)
bot.polling(none_stop = True)
Answer the question
In order to leave comments, you need to log in
You initiated it once and that's it.
If you need a new number every time - create it again
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question