How to delete a Telegram-bot user from db, when user delete the chat of the bot - aiogram

I am programming telegram-bot on Python, using aiogram and I need to delete user from database, when he delete the chat of the bot. I know that bot must receive some update, after user delete the chat of the bot. But my bot doesn't receive nothing messages about it. I tried sending a message to the user who deleted the bot hoping to get an error and then process it and remove the user from the database but the messages are still sent without error. Also I read about method sendChatAction, that can solve it, but I didn't find good examples with it. Any idea? P.S. I don't need advice how to delete user from database, I want to know how to get errors with info that user deleted the chat of the bot, that I can process(exception handling)

You can use the Update.my_chat_member updates. Those will tell you when a user blocks your bot. Note that your bot does not get notified if the user simply clears the chat history or deletes the chat with your bot without blocking the bot.

Maybe it will help someone. I use Flask-SQLAlchemy for working with database and aiogram for bot-development. Make sure that you import this "from aiogram.utils.exceptions import BotBlocked" for handling exception about blocking bot by user.
from bot_files.models import db, Users
from aiogram.utils.exceptions import BotBlocked
async def technical_works(bot):
users = Users.query.all()
for user in users:
try:
await bot.send_chat_action(user.telegram_id, action="typing")
except BotBlocked as exception_info:
db.session.delete(user)
print("User is deleted")
db.session.commit()

Related

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

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.

How to add multiple users in telegram channel using ImportChatInviteRequest in python

I'm having trouble adding multiple users to the telegram channel by using the invite link ImportChatInviteRequest in python.
I have tried the telethon documentation about ImportChatInviteRequest but it only adds me to the channel and since I'm already a member of the channel and the Admin I don't want to add myself.
from telethon.tl.functions.messages import ImportChatInviteRequest
updates = await client(ImportChatInviteRequest('AAAAAEHbEkejzxUjAUCfYg'))
I have tried the above code but like I said it only add me and I am the admin. Whenever I run this program this error shows up
Error: The authenticated user is already a participant of the chat (caused by ImportChatInviteRequest)
what I want to know is that is it possible to add multiple users using ImportChatInviteRequest and can you show it to me?

Telegram Bots: Can I now start a chat or not?

https://core.telegram.org/bots clearly says:
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first.
But if I visit https://telegram.org/blog/login it says
Telegram bots are a powerful communication tool, but until today they couldn't start a conversation. Even if you wanted them to reach out to you, you had to chat them up first.
With the new login widget, moving from interacting with a website to a conversation on Telegram becomes completely frictionless.
So it is somehow possible to start a conversation via the login widget. But how do I do it?
I get a user id. Is there an api endpoint to initiate a chat with the user?
The only way to begin conversing with a Telegram bot is to start the conversation with them yourself. There's no way for the bot to start a conversation with a user themselves.
As you said, telegram clearly says there is no way for bot to start a conversation, the only thing bots can do is sending messages to an old conversation (I mean a user have to start the conversation).

handle deleted message by user in telegram bot

Is there any way that can handle deleted message by user in one-to-one chat or groups that bot is member of it ?
there is method for edited message update but not for deleted message .
No. There is no way to track whether messages have been deleted or not.
The pyrogram have DeletedMessagesHandler / #Client.on_deleted_messages(). If used as Userbot, It handles in all chat groups channels. I failed to filter. Maybe it will work in a bot

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