skip to Main Content

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!

2

Answers


  1. 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())
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search