How can I send message to specific chatID? - telegram-bot

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

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

Get chat_id from bot to bot conversation

So What Im trying to accomplish is a bot that forwards messages from one specific user to one specific group chat. I started my testing by using my own chat_id and the bot perfectly forwards my messages to the right groupchat.
The problem is tho, that I want to forward messages that I receive from a bot. So I am trying to gather the chat_id between this bot and my bot but I can't get my head around how I should send and/or receive a message from this bot to gather said chat_id because I cant use it's #username in a /sendMessage? URL.
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.
Telegram Bots FAQ

Telegram Bot messages

Is it possible that bots send messages in chats only to specific members, and the messages aren't visible for other members of the chat?
For example:
There is a chat with 200 members and the bot is one of them. Somebody sends a command /help and bot responds only to him and others don't see the response
You can't do this at this time, but there are deleteMessage method you might interested.
And there are inline keyboard can ask users to START your bot, just put https://t.me/Bot as URL value.

How to receive messages in group chats using telegram bot api

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

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