How does telethon judge whether the chat is a group or a user? for bot - telethon

What I need is the type of this chat message, whether it is a personal conversation or a group conversation, not the interlocutor (I don't know if I say this, can you understand what I mean
https://docs.telethon.dev/en/stable/modules/custom.html?highlight=group#module-telethon.tl.custom.dialog
async for dialog in client.iter_dialogs():
if dialog.is_channel:
....
I looked through the documentation to find this, and after using it, it shows "telethon.errors.rpcerrorlist.BotMethodInvalidError: The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by GetDialogsRequest)
"
#lonami,
Is there any other way?

iter_dialogs method is only for user clients not for bot clients and you can use
await client.get_entity(<id or username here>) which gives your info about whether the id belongs to user or a chat or a bot.

Related

Telegram bot. Private messages to group members

There is a public telegram chat. I want to make it so that when a user joins a chat group, the bot will send that user a private message.
I checked the technical capabilities and realized that this cannot be done, but I could be wrong. To implement this feature, the user must run this bot, then such an opportunity will appear.
Question. How to make it so that before joining a chat group, a person has to launch a bot?
First, you can get an array new_chat_members of new users in the chat from Message. Then you can send a Message with button (InlineKeyboardButton). This button has a switch_inline_query_current_chat field. That's what you need.

Identify unique user in Dialogflow V1

So I am testing out Dialogflow and one of the first questions I have is: how does my bot know who it is talking to? I need to identify a user and keep that information for as long as I can. The basic scenario being:
User starts his/her first conversation.
Chatbot send a fulfillment request to the server trying to match a user within its own database.
The user is found, the information (as a JWT or some other token) is sent back to Dialogflow and stored there for further communication. In reality, this part would involve asking for user email, sending a verification code to that email and then verifying the user with the code.
User then starts chatting with a bot and all fulfillment requests get the unique token stored for this very user, so that my REST API knows which user is being served with the response.
Couldn't find anything about it in the docs (maybe I am looking in the wrong places).
There will be several integrations, like Messenger, Viber, Telegram. I dunno, maybe those APIs add some unique information on the user?...
Thanks for the help!
Sorry, I know it's been a while, but maybe this will help someone else.
The right solution here is a user id, not a session id. A user id is provided by the chat platform (Facebook, Slack etc) and is consistent across sessions for the same user.
To get the user id, go to the Fulfillment tab, enable the editor and use a function like so:
let r = request.body.originalDetectIntentRequest
//this makes sure that you're on an integration
if (r["source"]){
return r.payload.data.sender.id;
}
To tie together ids from different platforms, you probably have to have some kind of log-in process every time you encounter a new id on a platform.
Pop,
Sessions are built in already into DialogFlow requests to your fulfilment service, if you check the payload you will find a sessionId, it remains the same for the same client until it expires.
However if you want to identify the user from any of the clients that you can connect to DialogFlow like Messenger then from the same request payload to you you will notice that there is an object named originalRequest that is only available when requests are coming from those clients.
You can personalize those users response eg using their FB firstname in a message to them.

How to get authenticated by Telegram bot in Group chats?

Please kindly someone explain me how can the Telegram bot could understand who is sending the command in group chats and respond it with the the unique answer which is just for that user.
Surely in this case security issues should be considered and a user must not send command as another user.
I guess I can use username to send along with command.
Any suggestions...
The Message Object contains two objects apart from other objects:
Chat, message['chat'] which represents the Chat from which the message is coming. In your case the group.
User, message['from'] which represents the user that sent the message/command.
So it's easy to differentiate which user sent the message. And in case of Private chats, both the Chat object and the User Object are same.

Retrieve all chat ids using Telegram bot

the main question is how do I get the chat ids for all the conversations ever held with the bot?
Imagine that during the execution of the bot there is a conversation with the user A.
Now I stop the bot process and start it again.
How do I get the chat id of that past chat with the user A?
I understand you get the chat id when the user sends you a message, and you use that id to reply, but what if user A no longer sends messages to the bot during the current execution? how to get the past conversation id?
Is the only one option to store the ids and retrieve them when the second execution starts?
UPDATE:
Looks like the current solution is to store the chat id somewhere safe, as answered by #Tick Tock.
Your question is unclear to me but as I understand from your question I wrote something to you hope be helpful. You can retrieve chat_ids and use it to send something to that chat. I would give a sample code but before let me explain something.
In Telegram Bot API there is two definitions: chat_id and from_id.
1-When we are in private chat with some one chat_id and from_id are equal.
2-When our bot is a group member, then chat_id is id of that group and is different from that person id(from_id) may be send something to group(and maybe our bot receive it too-when privacy_mode is off)
I assume your bot is in private chat:
when user sends anything to your bot, then Telegram gives that message to your BOT( calls your script), this sample code sends "Hello chat_id" to that user.(in PHP)
define('BOT_TOKEN','12345:abcde');//replace with your bot token
$command_prefix_url='https://api.telegram.org/bot' . BOT_TOKEN ;
$update = json_decode(file_get_contents('php://input')); //retrieves data sent by telegram
$chat_id=$update->message->chat->id; //retrives `chat_id`
$rep = json_decode(file_get_contents($command_prefix_url . '/SendMessage?chat_id=' .
$chat_id . '&text=' . urldecode('Hello '.(string)$chat_id))); //send something to that `chat_id` (sender)
UPDATED: (due to edition in question)
First, chat_id is unique and always permanent for that user(in private chats)
even if your bot's user leaves your bot and rejoin again.
I don't hear or read anything up to now that Telegram have been provided a method to tell your bot WHOLE its users chat_id , so the best way to know about your users is to save their chat_id and other info (that you gather along the time from user from messages reciceve from) in a database.
If you save at least their chat_id in a simple database then you have a list of your bot's subscribed users. And since chat_id is permanent you can send anything you want to your users.
AND, as I understand from your question, IF you do not have a database but user A is your bot's subscribed user, AS I KNOW, you should wait until she/he send a single message to you, then you grab her/him chat_id and add it to your database. NOW you can send her/him every time anything.

How can I send message to specific chatID?

I have chatId and want send messages while execute some tasks from java code. I found this explain on telegram "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." But my task works good when I use https request directly from browser. My bot perfectly answer for requests but how I can do this without user request?
I can not truly understand your case!
Anyway, your bot can send a message to a chat ID Only if the user witch blongs to the specified chat ID added your bot (send start to a bot).
and if it happened then send your message with
/sendMessage