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

允我心安 提交于 2019-11-29 23:09:18
arlak
  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

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);
                }
            }
        }

    }
 }}

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");

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 at 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);
}

Good article to start: How-To: Send messages to Telegram from C#

TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp

mrtaikandi

I've written a client library for accessing Telegram bot's API and its source code is available in the Github. You can browse to the Telebot.cs file to see a sample of how to send a message to the bot API.

Github URL: github.com/mrtaikandi/Telebot

Nuget URL: nuget.org/packages/Telebot

Just look and learn how to make a POST HTTP request with your favorite language.

Then learn how to use Telegram Bot API with the documentation:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!