N
N
newbie_python2020-08-24 22:24:56
Python
newbie_python, 2020-08-24 22:24:56

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

2 answer(s)
S
soremix, 2020-08-24
@newbie_python

You initiated it once and that's it.
If you need a new number every time - create it again

U
UberPool, 2020-08-24
@UberPool

Well, when you click, generate and that's it ...

def generate():
    session =  uuid.uuid4()
    return session

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question