简体   繁体   English

如何使用我的电报机器人存储来自用户的输入,然后在需要时获取输入

[英]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.

相关问题 我如何在我的电报聊天机器人 python Telebot 中要求输入 - how i ask for input in my telegram chat bot python telebot 如何使用 python 在电报机器人中存储下一个文本 - how can I store the next text in telegram bot using python 如何从用户电报机器人获取信息? - How can I get information from the user telegram bot? 如何在我的 python discord bot 中存储输入? - How do I store input in my python discord bot? 如何在 Python 中存储来自命令行的用户输入? - How can I store user input from command line in Python? 我想用 python 为电报机器人编写一个计时器。 机器人从用户的 msg (str) 中获取“时间”。 如何将“时间”从 msg 转换为 (int) 类型? - I want to write a timer for telegram-bot with python. The bot gets "time" from user's msg (str). How can I convert "time" from msg to (int) type? 如何使用 Python 中的 Discord 机器人获取用户输入? - How do I grab user input with my Discord bot in Python? 如何为我的电报帐户创建电报脚本,而不是我的电报机器人 - How can I create Telegram script for my Telegram account, not my Telegram bot 如何在 python discord 机器人中获取用户输入? - How can I get user input in a python discord bot? 如何在 Python Discord Bot(重写)中获取用户输入? - How can I get user input in a Python Discord Bot (Rewrite)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM