How to receive messages in group chats using telegram bot api - telegram-bot

My telegram bot receives messages sent by user to my bot in private chats but not receives messages sent by users in group chats. Any options/api for getting group chat messages also,.

Talk to #botfather and disable the privacy mode.

Sequence within a BotFather chat:
You: /setprivacy
BotFather: Choose a bot to change group messages settings.
You: #your_name_bot
BotFather: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED
You: Disable
BotFather: Success! The new status is: DISABLED. /help

By default A Bot will receive only messages addressed to it by any user directly via posting by /command#YourBot any message you send.
After that it vill be available via getUpdates API call.
In browser it will be:
https://api.telegram.org/botToken/getupdates
Find the related message in output JSON and grab chatId. It will allow you to answer back with:
https://api.telegram.org/botToken/sendmessage?chat_id=123456788&text=My Answer

Make your bot by admin in group.

You can access all avaliable settings from all of your bots by sending /mybots to Botfather. Choose the bot, then Bot Settings and Group Privacy. If its disable (default), you can tap on Turn off.
Now its possible to receive the chat history using GetUpdates. This can be done via HTTP API or the frameworks. For example, in C# (.NET Core) like this:
var bot = new TelegramBotClient(ApiToken);
var updates = bot.GetUpdatesAsync().Result;
foreach(var update in updates) {
Console.WriteLine($"{update.ChannelPost.Date} {update.ChannelPost.Text}");
}
But keep in mind that this feature has some kind of perfect forward secrecy implemented. So you only get messages that were send after group privacy is disabled. As a result, the GetUpdates result is empty until some post was made.

if you added your bot before disable the privacy mode, you should remove the bot from the group and add it again

Related

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).

How to avoid Bot repeating command in groups?

I created a new Telegram Bot which maintains a simple list. When I chat with the Bot directly (in PMs) the Bot works as intended.
I created this bot for a group of friends, so I added the bot to the group, and now, whenever I execute a command, the bot responds, but also repeats my command. Is there any way to avoid this behavior?
I am using the python-telegram-bot API and sending messages using update.message.reply_text('My message')
Is this just intended behavior or is there any way to avoid the bot repeating the sent command?
The bot only is replying message, not creating 2 messages.
You should use send_message.
I found the solution. Telegram API understands this as quoting, and is enabled in groups by default, disabled in private chats.
It can be disabled by using:
update.message.reply_text("message", quote=False)

Telegram bot receiving commands from other bot

in the documentation of the telegram bot API I found:
Bots with privacy mode enabled will receive:
Commands explicitly meant for them (e.g., /command#this_bot).
General commands from users (e.g. /start) if the bot was the last bot
to send a message to the group.
So I created two bots - invited both in a group and had "firstbot" to fire /cmd#otherbot something commands. The "otherbot" echos everything it reads.
I (in the client) I can write - "otherbot" doesn't see it - which is correct due to privacy settings.
I i write /cmd#otherbot - "otherbot" receives and echos this - also correct.
BUT - when I let "firstbot" emit /cmd#otherbot in the group "otherbot" doesn't see it.
Am I doing something wrong - or am I miss-leaded by the documentation?
I use C# with Telegram.Bot by roundrobin.
Bots can't see messages meant for other bots, regardless of privacy modes.
Why doesn't my bot see messages from other bots?
Bots talking to each other could potentially get stuck in unwelcome loops. To avoid this, we decided that bots will not be able to see messages from other bots regardless of mode.
Recently I came across with this problem, that one bot can't read messages from another bot.
But I've found a solution: instead of using the Telegram Bot API, you can use TDLib (Telegram Database library) to read the messages.
I am using python, so with this short piece of code I am able to read messages from a bot:
from telegram.client import Telegram
tg = Telegram(
api_id=123456,
api_hash='api_hash',
phone='+555555555',
database_encryption_key='changehere' )
tg.login()
def new_message_handler(update):
message_content = update['message']['content']
message_text = message_content.get('text', {}).get('text', '').lower()
print(message_text)
# do what you want with the message
tg.add_message_handler(new_message_handler)
tg.idle()
I guess that the same can be extended for other languages.
With this approach combined with Bot Telegram API, you are able to:
read messages from a bot (with Telegram Database library)
send that message with another bot (with Telegram Bot API)

Send message to th all of bots users (telegram api)

how to send Send message to the all of bots users?
There is no way to sned message to all ?
what is the method name ?
From Official API FAQ:
How can I message all of my bot's subscribers at once?
Unfortunately, at this moment we don't have methods for sending bulk messages, e.g. notifications. We may add something along these lines in the future. (...)
Obviously, if you store users chat_id, you can send individual message to all users (I use this method).
Navid wants to send message to all subscribers via bot.
If subscribers are more than 100 persons bot will very slow to sending all messages and may doesn't send messages to all.
Navid's question was how we can send message without this problem?
you can send with curl_multi_exec
Currently, a developer would need to implement a special broadcasting function that would send the message to each active user at the time adding a small delay to avoid hitting the rate limit of 30 messages per second (see https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once). This would mean that a very popular bot with say 10K active users cannot give timely notification as the last user would get the message about 5 minutes after the first user.
Here is a feature request to ask to add a method in the Bot API to broadcast a message to all its active users at once. You can upvote this feature request. https://bugs.telegram.org/c/8463

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