I am writing a Telegram bot, to simplify the issue let's say you can ask the bot questions in a channel and it replies to you in private. When the bot receives the command it takes the chat_id of the user from: update.message.from_user.id
But when I do this I get an error:
Unauthorized: Forbidden: bot can't initiate conversation with a user
This sounds quite logical, as I assume this prevents from writing spam bots. The question is how do I overcome it? How can I let the user enable private messages from the bot?
Note: The bot does succeed sending messages to me but not to other users.
Telegram Bots can't initiate a conversation with a user.
Only an user can initiate it by clicking on the start button. There is no way to around this.
In your case, you can ask the user to start the bot before asking questions in the channel, for example by attaching a button containing the bot URL (https://t.me/bot_username).
message.from_user.id
try instead
message.chat.id
Related
I've seen some bot creators before with options to extract all members of bot who interacted (or started) bot before. but I can't find any method in telegram docs for extract users. Do you know any method for extract users of bot in any library or something?
I asked ChatGPT but he gives me a code for extract chat users:
async def get_users(client, message):
if message.text == "/users":
# get all users who have ever interacted with the bot
async for member in app.iter_chat_members(chat_id=message.chat.id):
user = member.user
if user.is_bot:
continue
if user.status == 'restricted' or user.status == 'kicked':
continue
How can I get UserID from user who started my bot before if I didn't save it?
there is any method for extract users of bot in any library or something?
How can I get UserID from user who started my bot before if I didn't save it?
You cant!
After you've passed an offset to the getUpdates call to remove any messages from the update queue, they're gone. Reff
There is no way to ask Telegram for a list of chat's that ever talked with your bot.
The only way of doing this is by a custom implementation where you save each chat id somewhere to recall later.
Can I use the discord API in python to harvest messages from a server (not my own server)? Assuming you have an invite link.
Thanks
Yes, it would be possible. If you look under the TextChannel section of the Discord Models section of the discord.py API Reference, there is the TextChannel history() method. This can be used to parse all the messages in a channel. If the limit argument of the method is set to None, then all the messages in the channel will be returned.
Since you say that the bot already has an invite, that implies they have access to a good part of the server. The method I mentioned requires the following permissions for your bot:
See Text Channels
Read Message History
Well, if your using a discord bot, you need them to invite your bot to their server.
Other than that you could theoretically listen with a bot on your own account but that would be against the discord TOS.
You can achieve this through a discord bot. Given that bots respond to events that fire within servers they reside in, you can listen in to incoming messages.
An example of such event can be found in discord-py documentation.
I'm trying to get the slack UserId of the user who initiated a conversation with a bot.
I tried using users.identity scope and api, but I'm only getting the UserId of the user who created the bot. The bot will be used by multiple users so I want to have an isolated chat for each user by getting the exact UserId of the user who is currently having a conversation with the bot.
I'm using Python, Amazon Lex and Slack.
Can somebody point me to the right path?
If the conversation is initiated as a direct message with the bot, you can subscribe to message.im event. You'll receive this event every time a user sends a message to the bot. In the response JSON body, event.user should provide you the userId.
It could be a different solution depending upon how you are making the app, and where do you need this information.
Is it possible to get a bot to send the first message in the chat without running a command like "/Start"?
Maybe it possible to save the chat_id of the requested user in advance and send him a message in the future, thus allowing the bot to send a first message in chat according to a predefined condition that will trigger him?
I searched for an answer online but could not find one.
Can you send me a link to a tutorial that explains how to do this?
Thanks in advance
Yoav Giladi :)
One solution is to use a Telegram Bot deeplink to provide an entry point to the Chatbot that does not require the users typing the /Start command.
After the very first interaction you can the obtain the chat_id which can be stored for using it to initiate a message later:
context.bot.send_message(chat_id, text='Howdy')
I want to make a telegram bot that people can send the bot a message if they first join a specific channel that the bot says
For example, if a user wants to send a message to the bot, the bot will tell the user that you have to join this channel then you can send me this massage
How can I write its code in python?
I've read related questions in this site but none of them answer my question clearly
thanks for your answers...
If you add the bot to the channel as admin, you can use getChatMember with the channel ID and the user ID.