I want to create a telegram bot for a home project and i wish the bot only talk to 3 people, how can I do this?
I thought to create a file with the chat id of each of us and check it before responding to any command, I think it will work. the bot will send the correct info if it's one of us and "goodbye" to any other
But is there any other way to block any other conversation with my bot?
Pd: I'm using python-telegram-bot
For the first part of your question you can make a private group and add your bot as one of its administrators. Then it can talk to the members and answer to their commands.
Even if you don't want to do so, it is possible by checking the chatID of each update that the bot receives. If the chatID exists in the file, DataBase or even in a simple array the bot answers the command and if not it just ignores or sends a simple text like what you said good-bye.
Note that bots cannot block people they can only ignore their
messages.
Related
My telegram bot database was accidentally deleted after I moved it to a new server.
Is there a way to obtain a list of all the numeric IDs of all users who have started the bot in the past? In reading Telegram's documentation, I did not find any solution (although I've seen someone do it before and I am certain it can be accomplished); I found out from searches that it should probably be accomplished on the client side and using something like getDialogs.
https://core.telegram.org/method/messages.getDialogs
Would anyone be able to assist me with that?
Is there a way to obtain a list of all the numeric IDs of all users who have started the bot in the past?
No, Telegram can't offer a list like that.
If an update is removed from the getUpdates() call, it's gone for good.
I found out from searches that it should probably be accomplished on the client side and using something like getDialogs.
The getDialogs you're looking at is part of the Telegram Core, used to create your own Client. This can't be used as a Bot.
So If you don't have a copy of your own, there won't be a way to tell all the users who have talked with your Bot, unless you ask them yourself ;)
I wrote a telegram bot using telegram bot python API, I want to implement two button option for user to select but I don't have any database to save the option with chat_id.
For example: A user want to translate a Eng text to another language, And has two languages available: 1.Persian 2.Spanish
user will select a language and then send the text and its choice will be saved for other texts
Can anyone help me to do that? Personally I thought that I can initialize a python dictionary with chat_id and its option but this is not practical.
Recently, I heard something about conversion handler that added in alpha version of telegram bot API, can I use it in this situation?
I'm developing a Telegram Bot via PHP and I want users to answer bot question.
For Example:
Bot: What is your name?
User: (Reply)
But I don't know how to process user reply for specific question from bot.
If I receive user reply via webhook, it doesn't contain last bot message, and I don't know what question this reply is.
Webhook has parameter callback_query, but it works only for messages that contains inline keyboard.
Can you help please?
This process is called conversation. There are various ways to implement this.
The best and common way is to implement a Finite State Machine which will save the current state and accordingly, have a conversation with a user. Famous libraries like python-telegram-bot already implement it, which can give you a general idea on how to do this. Other ways can be storing last message in a database, etc; which is not recommended.
I understand that this question has been asked before, but those topics are dated 4 years ago, so I wonder if anything changed.
TL;DR:
I understand the concern that sending messages via phone number would make users vulnerable for spam. But there is something I discovered that makes me reconsider this. If I /start a chat with Telegram Bot, chat_id is generated. Interesting thing is, this chat_id is the same for my conversations in other chats. I might be wrong here, but it seems that spam issue should be handled in a different way. Thus, I wonder if there is a possibility to send messages straight to user's phone number.
Thank you for your time.
No, you can't. Each user's ID is unique and not changing during conversation/chats. But you can't send the message to user if he did not accepted it by starting your bot as i know. Also, phone number can be shared only from user side. You can't find users by phone number, at least, official api doesn't provide such methods.
I wrote a piece of code which sends messages to a Telegram bot. In order to do so, I use the chat_id of the last conversation retrieved via the getUpdates method.
id = requests.get(f"https://api.telegram.org/bot{token}/getUpdates").json()['result'][-1]['message']['chat']['id']
My understanding is that the a conversation exists if someone started one with the bot via /start.
How can I initiate, from my code, a conversation to make sure a chat_id is available? (= that there is a conversion I can query).
I am also sure that the conversations, should they exist, are not kept indefinitely (this is another reason why requesting an update can yield empty results)
My understanding is that the a conversation exists if someone started one with the bot via /start.
Yes, conversation is always initiated by a user:
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use telegram.me/ links or username search to find your bot.
Note that /start is not the only option here.
If you try to send a message for user, who didn't start conversation with bot, you will receive something like this: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}.
How can I initiate, from my code, a conversation to make sure a chat_id is available? (= that there is a conversion I can query).
Normally, you shouldn't worry about that. Bot does not query specific user actions/requests with getUpdates, it queries all interactions from all users and then decides what to do according to internal logic you provide.
You may want to store information about users and/or their requests in a database every time you receive an Update from particular user in getUpdates.
Based on that, bot can make a decision to send for example, a message to him.
I am also sure that the conversations, should they exist, are not kept indefinitely (this is another reason why requesting an update can yield empty results)
Yes, docs clearly state that
Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.
An Update on a Telegram servers is an entity with a short life period.
If you haven't saved information about existing users, or lost a database, there's no way to retrieve that data from telegram servers.
P.S.: as a side note, I'd suggest using long polling, as Telegram Bot API is designed to be used with long polling if you're using getUpdates. The most important thing is the timeout request parameter of getUpdates method:
(timeout is) Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only.
As written in question, you're using short polling.