Simplest way to get a list of new posts from a telegram channel via API - telegram

I'm trying to write a simple server side script that will run every once in a while and check for new messages in certain Telegram channels. I've created a bot, but it looks like a bot can't me a member of a channel, so bot API is of no use here. So what I fugured I need to do is to write a very basic set of requests to do authentication with my own credentials and then fetch messages from certain channels I'm subscribed to and then check if any new messages are there.
Is it possible to do this on a very basic level with HTTP requests, without having to learn and use bot frameworks and such?

Related

Querying existing webhooks

An existing application has webhooks set up with the Telegram API.
A newer application want to use the same bot to send messages, eventually with the same named action webhook.
Initially, one would want to first query existing webhooks to identify existing ones, then possibly delete them, based on the increasing complexity of the operating context. The telegram API documentation does not appear to have methods for such actions.
A) can one query one's bot webhooks and delete them?
In any instance, the new server will have its own certificate for TLS communications.
curl -F "url=https://b.example.online/<WEBHOOKLOCATION>" https://api.telegram.org/bot<YOURTOKEN>/setWebhook
B) If one sets a new webhook with the same action (or webhooklocation in Telegram-speak) name, does that kill the existing ones, say
curl -F "url=https://a.example.online/<WEBHOOKLOCATION>" https://api.telegram.org/bot<YOURTOKEN>/setWebhook
or will the notifications be sent to two webhooks (i.e. the combination of certified URL and action name are considered a unique object) ?
C) what happens to the basic webhooks /start /help etc. ? Are they responded to by the Telegram servers if webhooks not present? Again, can they have multiple certified domain destinations to the same action
One avenue is to revoke the token #BotFather which probably kills the related webhooks. The new token allows webhook creation anew.
There is a deleteWebhook action though not documented at the official documentation link
Through trial an error, the conclusion is that only one webhook exists per bot. Having set different webhooks for different apps with the same bot, only the last one is active (the old one is deleted).

Is there an option to view conversations\messages sent by my telegram bot

I have just created a bot on telegram. The purpose is to create private conversations with users (each user separately)
With my facebook bot I can go into the Page "Inbox" and watch all conversations. Is there such an option on telegram ? This helps me alot debugging my system.
To "watch all conversations" you can store them to database (e.g. csv file, SQLite, Postgresql, MongoDB, or any other depending on your environment). And later you can query for them when you need.
Another option, you can create separate predefined chat with you and bot, where bot will write some logs. Or if you have time - you can develop advanced mechanism that will show history (from database mentioned above) and will have ability to reply directly to that chat by chat_id.
I used rocket chat for that. I was creating private room for every user and was adding there the user and the bot (I was doing that via my backend). Then I used rocket chat ui to view the conversations. Currently, I see they have built-in integration with telegram bots: https://docs.rocket.chat/guides/administrator-guides/integrations/telegram

How do I send & receives Telegram messages programmatically?

I want to create an app that sends telegram messages to different people by phone number, and/or telegram id.
I've heard about the "Telegram Bot API", but from my understanding it has some limitations (like it can't send messages unless users authorize it and send the first message to it, and it doesn't support sending messages using phone number)
And there is the "Telegram API", which looks like a lot of work and coding..? I'm a 1 man developer and I don't want to spend years to create a Telegram app from scratch that talks to telegram servers and does all the protocols and then I have to update it constantly and so on and so fourth.
I just want something that is easy to code & maintain, and can use my telegram account to programmatically send/receive messages. That's it. Something like :
send.message(phone number, message)
or some sort of REST API that let's me authenticate my telegram account and then provides methods that I can use to send messages to contacts.
Any ideas on how I can implement this? Any pointers would be appreciated!
Have a look at MTProto libraries like telethon or pyrogram. They are user-friendly (with lots of helper functions to abstract raw telegram api calls and their interfaces somewhat resemble the telegram bot api)
Here is a sample code (from the telethon docs):
from telethon import TelegramClient
# Remember to use your own values from my.telegram.org!
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
# You can send messages to yourself...
await client.send_message('me', 'Hello, myself!')
# ...to some chat ID
await client.send_message(-100123456, 'Hello, group!')
# ...to your contacts
await client.send_message('+34600123123', 'Hello, friend!')
# ...or even to any username
await client.send_message('TelethonChat', 'Hello, Telethon!')
with client:
client.loop.run_until_complete(main())
Actually, telegram did a decent job to provide a powerful yet very simple API for its Bots (Attention: You can also use telegram applications like MadelineProto or telegram CLI to send messages and this is different from using Bot APIs. The applications are like your mobile/desktop app and use a real account with a particular authenticated phone numbers and you can do mostly anything that you can do with your phone app. But this is a different story).
For sending messages using bots, you can find all the documentations here. To use these APIs, you need:
Create a bot using Bot Father robot: he will help you to create a robot and will eventually give you some secret token that you should use with your robot
Some users should "/start" your robot. After starting a robot or any communication from user, you have two different options: (i) you can read the updates by a polling the updates from Telegram server using update method or (ii) setting a webhook: this way, telegram will call that webhook to make you inform about the message. Either way you use, to send message to an account you need the so called "chat_id" of that user (and you should obtain it using one of the mentioned approach)
The final step is to send a message: for the simplest case, you want to send a simple text message to a person with some particular chat_id. You only need to call this URL (i.e. the API):
https://api.telegram.org/bot{BOTTOKEN}/sendMessage?chat_id={CHAT_ID}&text={SOME_TEXT}
With this sort of calling REST api, you can call many of the functions and this is only the first step and you can do much more complex and fun stuff! For many languages, there are some packages to work with the API which make everything simple and abstract to developers. A list of most popular packages could be found here.

can Telegram CLI mimick Telegram bots?

I saw on internet that there is some programs that can use Telegram CLI.
I want to choose between them
for Telegram bot API there is much more documents explaining its functionality, but for CLI there isn't much that explains its features
it seems the only way to know about is to experiment with it.
unfortunately i don't have a linux distro installed on my pc so experimenting isn't an option right know.
so I thought to ask from people who already used it
I know about Telegram bot api, its powers and its limitations
here is my questions:
what can I do using Telegram CLI that I can't do with with bot api, and vice versa?
Telegram bot API:
do not require to register new Telegram account, so you don't need to have another phone number;
bot cannot write to user first, only after user sends a first message to bot;
already has the commands interface (/command);
can do stuff by simple HTTP POST (by sending request via cURL, for example). So you can hook to this tons of stuff (notifications about new articles at the website or so);
you can rather easily create lots of them;
you can write you own implementation in almost any programming language;
you have a list of bots you have created (thanks to #BotFather). If you have lost somehow control of your bot - just revoke the token via #BotFather and it's yours again;
pretty simple to use.
Telegram client application (in this case - CLI):
requires new Telegram account registration with phone number;
acts like any other actual Telegram user (can write to other users first, without invitation). Well, that's a good thing;
not really good cross-platform abilities for now (some lack of CLI-realisations);
not really easy to install and use;
you have to implement the commands handling part;
if you have lost your phone number - pretty much you lose this account, because it's not like you would make some precautions for CLI client account. So you will have to register a new one and repeat the setting procedure for client.
In conclusion, bots actually got rid of CLI clients, in my opinion. I had CLI account right to the moment the bots appeared. After that I deleted it and created a bot. And not just one :)
So, bots are for the "robot" stuff, and real accounts are for the real people.
The Telegram CLI library interacts directly with their MTproto protocol, which means it's like their desktop/mobile app...but for the console. You can send messages from one phone number to another. This means that when authenticating with the CLI application, you use your real number as if you were logging into the mobile application.
I wouldn't suggest using it for bot behavior as you have to write an application that wraps the Telegram CLI and parses the log file as it is displayed...since it doesn't implement all the MTproto methods and the outputs for the log file are custom...it can be quite annoying and different than what you would expect.
With Telegram CLI you can send a number to another Telegram user without having them initiate the conversation first (since it functions just like a normal Telegram client) where the Bot API requires the user to add the Bot and start interaction before the Bot knows you are there.
previous answers are almost correct. Two different interfaces for different purposes:
Telegram Bot API allows to develop a Telegram Bot.
Telegram CLI (as this one, as an implementation example: https://github.com/vysheng/tg) are telegram client implementations, running from command line, based on MTproto protocol, as Chris Brand said.
As a bot developer, I'm interested to have a TG CLI interface (2) to automatize dialogs tests, with bash scripts, with a bot made with (1).

How to tell different clients with SignalR server push?

I just finished one SignalR sample, the well-known Chat sample.
This sample just broadcast a chat message from one client to all the clients. What if we want to send message to only a specific client?
(I guess there should be some ID to identify each client. These IDs should be stored on server when clients subscribe to the server. And server can choose which ID to push message to. )
You have different way to map your user with a connection. You can compare the different ways in this tutorial depending on your requirements.
Another solution is to define 1 group per userId and notify the group when you want to notify a user (link). Be careful, groups are not secured.
Like Daniel describes you can use a group or use the hubcontext to get the context for a specific connection using the connection Id.
var client = context.Clients.Client(connectionId);
There are also several libraries that abstract SignalR, some of these comes with their own way of calling specific users.
I have made a library like this which is based on the Event aggregation pattern. It comes with a API to let you create code that determines which clients should receive a specific event
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki/Implement-constraint-handlers
Here is also a blog post I made showing how you can achieve declarative role authorization with my library, maybe it can give you some ideas.
http://andersmalmgren.com/2014/06/12/client-server-event-aggregation-with-role-authorization/

Resources