Telegram Bot API errors codes (integrating Groups and Channels) - api

I'm working on my open-source project BOTServer a http://telegram.org Bot Platform Webhooks Server, for Rubyists.
I experienced strange error when sending messages (sendMessage API endpoint) on a Telegram Bot I created. when sending messages to a specific Bot I created, I got these errors:
Error code 400 -> Bad Request: group is deactivated
Telegram::Bot::Exceptions::ResponseError - Telegram API has returned the error. (ok: "false", error_code: "400", description: "[Error]: Bad Request: group is deactivated")
and/or
Error code 403 -> Bot was kicked from a chat
Telegram::Bot::Exceptions::ResponseError - Telegram API has returned the error. (ok: "false", error_code: "403", description: "[Error]: Bot was kicked from a chat")
That's really strange.
It seems that the problem is related to the "story" of a specific Bot (errors are not systematic and I do not have issues with another bot). In facts I done these steps:
I create the bot (lets cal it: MYbot)
I ADDED the bot to a Telegram Group (e.g. MYGroup)
I REMOVED the bot from the Group
I send messages from the bot in reply to a user (then I got errors)
Question 1
Ok I'm pretty sure something wrong happened Telegram Serve side and I could delete and rebuild the bot... Any better idea about a solution ?
Question 2
There is a published list of Telegram Bot errors ? I didn't find in Telegram Bot documentation/tutorials.
I love Telegram architecture! But I feel there are some issue/bugs with Bots APIs especially in recent possibilities to insert Bots in Groups and Cahnnel. Any similar experience ?

I answer myself having found a possible reason of the problem. The problem arise just with a single specific Bot I created in past (and I removed from a group). As specificed in question:
Step by step story
step 1:
After creation I added that bot on a group with 2 users: (myself and a second person).
step 2:
Afterward I removed that bot from the group.
step 3:
I used that bot to test my BOTserver
sending SendMessage od reply to webhook update.
Now i experienced the mentioned 400/403 errors.
step 4:
I DELETED that Bot, chatting with BotFather.
step 5:
I created again the same Bot with the **SAME identichal name*
Step 6:
I tested again, an I rised the usual issue:
Telegram API has returned the error. (ok: "false", error_code: "401", description: "[Error]: Unauthorized"):
Step 7:
I created new bot(s) with different name(s)
Finally, no more errors!
Conclusion:
I fair the original Bot name remained in a "wrong" Telegram server internal status. My hypotesis is that adding/removing Bots from Groups have some problem/bug.
My Solution:
delete the bot name created problems, no more using that "name" :(
recreate a bot with different name.
Any similar experience ?

I,m getting this error through WebHook requests.
This issue occurs after adding bot to a group and disable bot Add to group feature via BotFather.
This issue exists until i fetch updates manually by /getUpdates method and make them passed for Telegram server and after that WebHook works fine.
Hope this works for you too.

return ok if bot API return respond error, like this example
try {
return Telegram::sendMessage($message);
} catch (TelegramResponseException $exception) {
Log::info($exception->getMessage());
return ['ok' => 'true'];
}

Related

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)

Telegram bots - how can we detect that a user has started typing?

We can get Telegram messages when they are complete and sent via long polling or webhooks, but is there a way to understand when the user has started typing?
I guess this is not a normal case for bots and I dont think there is a straight away method but could we fall back on the Telegram API for messaging apps for that?
Well, actually I found a solution in the current Bot API; inline queries. While they are not intended for this, inline queries send what the user types in to the bot in real-time, so you are alerted as soon as the first characters are typed in.
There is a way in Telegram Core API, named: updateUserTyping.
But there is not any way in Telegram Bot API yet (until today: 2016-10-17).
If you type or send a file to the bot during these processes you do not get any message from Telegram in your webhook, but it's possible they'll add a new feature to detect user typing in future Bot API updates.

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

How to know if user has seen a message sent by my bot in Telegram?

I'm developing a Telegram bot, and I want to know whether a user, that has started a private chat with my bot, has seen a certain message sent by bot, and to know when has he seen it.
Is it possible to do so?
Thank you very much.
That's currently not possible.
I'm using this solution.
Create a channel specially for your bot
Let your robot send message from this channel to users, you can use forward method.
Now you can see how many users have seen your message
Telegram Bot API has limit functionality. There is absolutely no way to get message view count at the time of this comment.
Usually TDLib can be used instead of Telegram Bot. It is Telegram client library. You can use it directly or make it as services for BOT to call.
For example. use TdApi.GetMessage to get the message, and the returned message has field of interactionInfo which contains forwardCount or viewCount.
perhaps this answer help you :
in the end of your message place
link example : yoursite.com\checkvisit.php?id=1234
when user open message link automatic run for telegram ...
you can understand message was read
you must in checkvisit.php set to check db if id exist and not read set it to read
then id in db remove or disable
but this method simple - telegram must add 1 parameter to return this
sorry my english not good
it is not possible yet , you might want to search for Madeline bot

YouTube Data Api v3 - subscriptions.list and mine = true

I found following problem.
If user is authorized with youtube and you call subscriptions.list with parameter mine set to true. Then you get a list of subscriptions of authorized user. This works just fine if user has YouTube account linked with g+ page or if user has its own channel on YouTube. But if it is just a normal user without a channel and g+ page then this request returns "subscriberNotFound" error.
Is this a bug or am I doing something wrong?
You need to have a channel to subscribe, so that's normal.
You can read this blog for working with channel ids.
Also please use stackoverflow for only programming issues.
As suggested here, you should file bug reports or features request in public issue tracker.
The call to subscriptions.list can't return a subscriptionNotFound Error. This is an error which only occurs when you try to delete a subscription. Maybe you are calling the wrong method? Take a look at this link for more information on the possible error codes, and details why they occur. For all Error codes look here