[英]How can i store input from a user using my telegram bot and then fetch the input when i want
I want my BOT to ask the user a question.我希望我的 BOT 向用户提问。
Let's say I have an empty string named "email"假设我有一个名为“email”的空字符串
And I set email=''我设置 email=''
Let the question be the user's email.让问题是用户的 email。 So the bot will ask the user to type his email, then whatever the user types next after the question, the bot will save that reply and update the email.
因此,机器人会要求用户输入他的 email,然后无论用户在问题后输入什么,机器人都会保存该回复并更新 email。
I already know that by default any reply/message sent to a telegram bot is stored and you can retrieve it by using the getUpdates method.我已经知道,默认情况下,发送到电报机器人的任何回复/消息都会被存储,您可以使用 getUpdates 方法检索它。
Let's say the user replied: "name@Gmailcom"假设用户回复:“name@Gmailcom”
So now I want to store this reply in the variable 'email'所以现在我想将此回复存储在变量“电子邮件”中
So the variable 'email' will now contain the value 'name@Gmail.com'所以变量“email”现在将包含值“name@Gmail.com”
I want the bot to contain an account section.我希望机器人包含一个帐户部分。 So when the user clicks this button it will diplay something like this:
因此,当用户单击此按钮时,它将显示如下内容:
User:
Email:
No of Referrals:
So when the user clicks on the button "ACCOUNT" it will display:因此,当用户单击“帐户”按钮时,它将显示:
User: JOHN
Email:
No of Referrals:
#Since email='' therefore email is blank.
Now when the user updates his email and then goes back and clicks "Account" we now have:现在,当用户更新他的 email 然后返回并单击“帐户”时,我们现在拥有:
User: JOHN
Email: name@Gmail.com
No of Referrals:
Also please is it possible immediately after the user updates his email and sends the reply, then the BOT will reply with a message like:另外请在用户更新他的 email 并发送回复后,是否可以立即回复,然后 BOT 会回复如下消息:
Thank you for updating your Email address
Your email is : name@Gmail.com
Also if you have idea on how to create referral link using telegram bot, please share it.另外,如果您对如何使用电报机器人创建推荐链接有想法,请分享。
How can I do this.我怎样才能做到这一点。 I'm using pyTelegramBotAPI.
我正在使用 pyTelegramBotAPI。
Easy answer:简单的回答:
You need a data base你需要一个数据库
Eg I have developed a budgeting telegram bot that stores daily purchases and analyses spending.例如,我开发了一个预算电报机器人,用于存储日常购买并分析支出。
I store all user data in a separate data base (in this case redis
) and access it later.我将所有用户数据存储在一个单独的数据库中(在本例中
redis
)并稍后访问它。
Given your use case I would recommend a simple key-based data base like redis
where you can store and access simple dict-like data.鉴于您的用例,我会推荐一个简单的基于密钥的数据库,例如
redis
,您可以在其中存储和访问简单的类似 dict 的数据。 Heroku and other web services that are popular for hosting telegram bots also do offer free redis
instances. Heroku 和其他用于托管电报机器人的 web 服务也提供免费
redis
实例。
Here is a sample code from my bot这是我的机器人的示例代码
# Load/create pickle and add new record, afterwards save pickle
try:
user_id = str(update.effective_user.id)
db = pickle.loads(r.get(user_id))
db.append(entry)
pdb = pickle.dumps(db)
r.set(user_id,pdb)
except:
user_id = str(update.effective_user.id)
db = list()
db.append(entry)
pdb = pickle.dumps(db)
r.set(user_id,pdb)
update.message.reply_text('Saved!')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.