So I have a bot which moves from stage to stage using the stage names as a product of return of each function. e.g.
JavaScript
x
27
27
1
STEP_1, STEP_2, STEP_3 = range(3)
2
3
def askforfile(update, context):
4
chat_id = update.message.chat_id
5
context.bot.send_message(chat_id=chat_id, text="give a file!")
6
return STEP_1
7
8
def getfile(update, context):
9
chat_id = update.message.chat_id
10
filename = 'newfile'+str(chat_id)+ '.txt'
11
f = open(filename, 'wb')
12
context.bot.get_file(update.message.document.file_id).download(out=f)
13
f.close()
14
return STEP_2
15
16
def get_info(update, context):
17
chat_id = update.message.chat_id
18
info = update.message.text
19
return STEP_3
20
21
def end(update, context):
22
with open(filename, 'rb') as f:
23
writecsv1 = some_module.some_function(filename, info)
24
with open(writecsv1, 'rb') as doc:
25
context.bot.send_document(chat_id=chat_id, document=doc)
26
return ConversationHandler.END
27
So in the end() function I need to pass the variables from functions getfile() and get_info() in the some_function. But I have no clue how to do it, because returning multiple values doesn’t work for me and even if it worked, obviously I can’t call functions.
Advertisement
Answer
You could use global variables filename and info (maybe don’t name them that) that you rewrite within your functions. Just define them with some default value above your function definitions. Right now they’re just defined locally and disappear once the function returns.