Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Telegram API in C# to send a message

Tags:

c#

api

telegram

I want use Telegram API in C# for send a simple message to a number. I found some lib's on GitHub but I am not able to use them.

Can anyone give a simple code ? Can I simply make HTTP calls ?

like image 942
Hadi Delphi Avatar asked Jul 07 '15 14:07

Hadi Delphi


People also ask

How do I interact with API in Telegram?

You will have to visit https://my.telegram.org/ and login with your phone number and confirmation code which will be sent on Telegram, and fill in the form under “API Development Tools” with an app title and short name.

Can we use Telegram as an API?

We offer two kinds of APIs for developers. The Bot API allows you to easily create programs that use Telegram messages for an interface. The Telegram API and TDLib allow you to build your own customized Telegram clients.

How can I get Telegram API?

Obtaining api_id Sign up for Telegram using any application. Log in to your Telegram core: https://my.telegram.org. Go to "API development tools" and fill out the form. You will get basic addresses as well as the api_id and api_hash parameters required for user authorization.

What is the URL for Telegram API?

Introduction to the Telegram Bot API The first part of the URL indicates that you want to communicate with the Telegram API ( api.telegram.org ).


4 Answers

  1. Install-Package Telegram.Bot
  2. Create a bot using the botfather
  3. get the api key using the /token command (still in botfather)
  4. use this code:
var bot = new Api("your api key here");
var t = await bot.SendTextMessage("@channelname or chat_id", "text message");

You can now pass a channel username (in the format @channelusername) in the place of chat_id in all methods (and instead of from_chat_id in forwardMessage). For this to work, the bot must be an administrator in the channel.

https://core.telegram.org/bots/api

like image 167
arlak Avatar answered Oct 02 '22 06:10

arlak


Here is the easiest way I found so far. I found it here, thanks to Paolo Montalto https://medium.com/@xabaras/sending-a-message-to-a-telegram-channel-the-easy-way-eb0a0b32968

After creating a Telegram bot via BotFather and getting your destination IDs via https://api.telegram.org/bot[YourApiToken]/getUpdates you can send a message to your IDs by issuing an HTTP GET request to Telegram BOT API using the following URL https://api.telegram.org/bot[YourApiToken]/sendMessage?chat_id=[DestitationID]&text=[MESSAGE_TEXT]

Details on a simple way to create a bot and get IDs may be found here: https://programmingistheway.wordpress.com/2015/12/03/send-telegram-messages-from-c/

You can test those url strings even directly in browser. Here is a simple method I use in C# to send messages, without dependency on any bot api related dll and async calls complication:

using System.Net;
...
public string TelegramSendMessage(string apilToken, string destID, string text)
{
   string urlString = $"https://api.telegram.org/bot{apilToken}/sendMessage?chat_id={destID}&text={text}";

   WebClient webclient = new WebClient();

   return webclient.DownloadString(urlString);
}
like image 32
VVP Avatar answered Oct 02 '22 05:10

VVP


use this code :) with https://github.com/sochix/TLSharp

 using TeleSharp.TL;
 using TLSharp;
 using TLSharp.Core;

 namespace TelegramSend
 {

    public partial class Form1 : Form
    {
      public Form1()
     {
         InitializeComponent();
     }


    TelegramClient client;

    private async void button1_Click(object sender, EventArgs e)
    {
        client = new TelegramClient(<your api_id>,  <your api_key>);
        await client.ConnectAsync();
    }

    string hash;

    private async void button2_Click(object sender, EventArgs e)
    {
        hash = await client.SendCodeRequestAsync(textBox1.Text);
        //var code = "<code_from_telegram>"; // you can change code in debugger


    }

    private async void button3_Click(object sender, EventArgs e)
    {
        var user = await client.MakeAuthAsync(textBox1.Text, hash, textBox2.Text);
    }

    private async void button4_Click(object sender, EventArgs e)
    {

        //get available contacts
        var result = await client.GetContactsAsync();

        //find recipient in contacts
        var user = result.users.lists
            .Where(x => x.GetType() == typeof(TLUser))
            .Cast<TLUser>()
            .Where(x => x.first_name == "ZRX");
        if (user.ToList().Count != 0)
        {
            foreach (var u in user)
            {
                if (u.phone.Contains("3965604"))
                {
                    //send message
                    await client.SendMessageAsync(new TLInputPeerUser() { user_id = u.id }, textBox3.Text);
                }
            }
        }

    }
 }}
like image 15
Zokirov Rakhmatilla Avatar answered Oct 02 '22 06:10

Zokirov Rakhmatilla


1-first create a channel in telegram (for example @mychanel)

2-create a telegram bot (for example @myTestBot) and get api token for next step

3-add @myTestBot to your channel(@mychanel) as administrator user

4-use below code for send message:

   var bot = new TelegramBotClient("api_token_bot");
        var s = await bot.SendTextMessageAsync("@mychanel", "your_message");
like image 6
habiat Avatar answered Oct 02 '22 04:10

habiat