Cannot delete bot message in telegram - telegram

I sent a message using telegram bot api. The bot I'm writing is kind of private channel bot, not inline.
So I've just sent a message, 48 hours has not lasted.
I tried to use this: https://api.telegram.org/botTOKEN/deleteMessage?chat_id=CID&message_id=MID
As a result I got "Bad Request: message can't be deleted"
Why so?
In my case I'm asking user for a price. If the price is not valid, I send a notification, asking user to re-enter the price. So, there might be many service messages, asking user to correct the price. After he entered correct value I want to erase these service messages. I can't think of any other solution except removing these messages, but I couldn't manage to do this.

Related

401 Unauthorized error after a year of successful using of Telegram API

Today when I as usually run my client to read income messages of my Telegram Bot I get the error
{"ok":false,"error_code":401,"description":"Unauthorized"}
I had been successfully used Telegram API every day during a year and never seen this error. I also didn't change any settings
What may happened?
At the page https://core.telegram.org/api/errors I read it's possible to get more detailed error description like AUTH_KEY_UNREGISTERED, AUTH_KEY_INVALID, USER_DEACTIVATED and so on.
But how can I do that?
Is it possible just to add something in the address bar to the request like https://api.telegram.org/bot<MY_TOKEN>/... ?
It may be helpful for someone.
I was not using my main account of Telegram associated with my phone number more than a year. That's why Telegram deleted my main account and accordingly my Telegram bot.

Make Telegram bot to read only /commands as administrator

I am using Telegram bot API with AWS Lambda and Zappa.
Everything was working fine. Until I realised that bot's webhook is being called everytime whenever a message is sent by anyone in the group.
I want to limit this webhook requests, as Lambda allows for only 1 million calls which is sufficient if bot is invoked, but only when /commands are called.
As I have to pin and edit messages using bot, that's why administrator permission is must.
I just want my bot to respond only to /commands along with administrator rights.
Please help me in tackling this issue.
I am assuming you added your bot to a Telegram group. In this case, the first thing that you need is group id. This should help: https://github.com/GabrielRF/telegram-id#web-group-id
And then you need to write a function that identifies the status of a chat member. You can do this using getChatMember method in Telegram Bots API.
If the chat member's status is "creator" or "administrator" then the response is sent to that user.
There are 2 things you need to do.
setPrivacy for your bot - go to BotFather chat and use the /setPrivacy command which gives you the ability to set:
'Enable' - your bot will only receive messages that either start with
the '/' symbol or mention the bot by username. (Should be default)
'Disable' - your bot
will receive all messages that people send to groups. Current status
is: DISABLED.
When setting the commands for the bot, set the BotCommandScopeAllChatAdministrators scope which should limit the use of the command only to the admins of the chat.

what can be done by a telegram chat ID?

if someone find my telegram chat ID (instead of my phone number or username), what can do with that?
is it dangerous?!
is it a big deal that someone can find my ID? should I worry about it?
If someone with a (user)bot wants to send you a message, they can do that via the userID only. But only if they have "seen" you. Seeing you is considering their client to receive a message from you, be it in a private message or group.
Bots can only send you a message if you have started it in a private chat, otherwise they can only send you a message in a group they share with you.
So there is no real risk of people knowing your ID.

how to check if a user started a bot in telegram or not?

I'm developing a bot in telegram , here is my problem:
I have some posts in my channel which some inline_keyboards are attached to them. when a user press the button , next events happen in a bot and some messages show to a user. if user has been started the bot before , there is no problem , if not I have error. how should I check the user is member of my bot or not to handle this problem???
try to use sendMessage() API and send a message to user. After that telegram will response a status tell you whether success or not.
if started bot receive receive is ok,
if not started bot receive is nothing.
{"ok":true,"result":{"message_id":9999,"from":{"id":00000000,"is_bot":true,"first_name":"mybot","username":"mybot"}}}
like that, good luck.
There is no way to check if bot can talk to user or not without send message.
You can use url with https://t.me/Bot?start=some_data format, and you will receive /start some_data, please try this link yourself.
The problem with this approach is that if a lot of users do the same, telegram starts to throttle your callbacks :( ... it would be so much easier if you could just read that information somewhere...

How to echo message at telegram /start?

I'd like to set up a authentication through telegram using it's deep linking api.
In order to authenticate, in my app I ask users to click on a link like:
https://telegram.me/myloginbot?start=somesecretkey
If I understand the docs correctly, I should expect the bot to echo back somesecretky to my server.
Now, this step of the docs is unclear to me:
Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start
If I understand correctly, I need to configure myloginbot so that when the user clicks start button on the bot's page, the bot echos back to my server a url containing somesecretkey and some user info. But I don't know how to do so.
In this answer, it is suggested that:
Let the bot retrieve the username by querying the database or key-value storage for unique_code.
But I don't know how can I make the bot query the (presumably remote) database.
So really appreciate your hints.
My understanding to deep linking is this:
You have a database of users. Each user has an ID. Suppose you want your Telegram bot to communicate with user 123. But you don't know his Telegram chat_id (which the bot needs in order to send messages to him). How do you "entice" him to talk to the bot, thus revealing his chat_id? You put a link on a web page.
But the link has to be "personalized". You want each user to press on a slightly different link, in order to distinguish them. One way to do that is to embed user ID in the link. However, user IDs are not something you want to expose, so you generate a (temporary) key associated with each user ID, and embed that key in the link. For example, user 123 has the key abcde. His personalized link will be:
https://telegram.me/myloginbot?start=abcde
Someone clicks on the link, and is led to a conversation with your bot. At the same time (or when he presses the START button), your bot will receive a message:
/start abcde
On receiving that message, the bot sees that abcde is associated with user 123. Telegram chat_id can also be extracted from the message. Now, the bot knows user 123's chat_id, and can send him messages afterwards.
To experiment with deep linking, you need a bot that can handle /start messages, supported by a "datastore" that remembers the key-ID associations. When Telegram docs say "memcache", they just mean something that stores the key-ID associations. For an experiment, it may be as simple as a dictionary, or an associative array. In real life, it may be Memcached (the memory caching software), or a database table.
If you use Python, I recommend taking a look at telepot, a Python framework for Telegram Bot API. It does not do deep linking per se, but it does help you in receiving messages for a bot, and other bot operations in general. I also have an example there demonstrating how to output a personalized link, set up a webhook, and parse the incoming /start command with the key.

Resources