Git Product home page Git Product logo

Comments (19)

MrRoundRobin avatar MrRoundRobin commented on May 18, 2024

why do you want to switch to webhooks?

i started to code an example web service but it took longer than i thought currently i have no time to finish it.

from telegram.bot.

Navid-Sa avatar Navid-Sa commented on May 18, 2024

I want webhook because im writing a webservice and i don want those get update and while... things..

from telegram.bot.

Navid-Sa avatar Navid-Sa commented on May 18, 2024

if you think that webservice that you are working on can help , can you send it to me?

from telegram.bot.

ScottRFrost avatar ScottRFrost commented on May 18, 2024

I'd also like to see a ASP.NET Web API example.

@MrRoundRobin The reason I want to use it is pushing the updates from their server uses less bandwidth and CPU time than constantly pulling every second with my console app. Plus, it's nice to support both approaches since the bot API offers both.

from telegram.bot.

MrRoundRobin avatar MrRoundRobin commented on May 18, 2024

I've created an example using OWIN and WebAPI, see 7ceaade

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

@ScottRFrost did you get the webhook to work on your side? I've tried following the example on 7ceaade, but so far I can only reach my Post with REST testing.

@MrRoundRobin I've followed the example using my web application, the code block is below. On the Post method in my controller I've done the same thing on your example but no success.

void Application_Start(object sender, EventArgs e) {
      // Code that runs on application startup
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      // Setting up webhook
      Telegram.Bot.Api bot = new Telegram.Bot.Api("mytoken");
      bot.SetWebhook("https://myhost:8443/api/webhook").Wait();
}

If this information is relevant, my application is hosted on godaddy using a self-signed certificate.

Many thanks in advance.

from telegram.bot.

MrRoundRobin avatar MrRoundRobin commented on May 18, 2024

If you use a self-signed certificate you musst upload it:

var cert = new FileToSend("certificate.cer", new FileStream("path\to\certificate.cer", FileMode.Open));
bot.SetWebhook("https://myhost:8443/api/webhook", cert).Wait();

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

Still doesn't seem to work.

void Application_Start(object sender, EventArgs e) {
      // Code that runs on application startup
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      // Setting up webhook
       var cert = new Telegram.Bot.Types.FileToSend("certificate.pem", new FileStream(Server.MapPath("~/Resources/certificate.pem"), FileMode.Open));
      Telegram.Bot.Api bot = new Telegram.Bot.Api("mytoken");
      bot.SetWebhook("https://myhost:8443/api/webhook").Wait();
}

I've looked up, and it seems Telegram has no problem with .pem certificates, which is the one I can download from godaddy.

from telegram.bot.

MrRoundRobin avatar MrRoundRobin commented on May 18, 2024
bot.SetWebhook("https://myhost:8443/api/webhook").Wait();

must be

bot.SetWebhook("https://myhost:8443/api/webhook",cert).Wait();

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

Sorry, I've pasted the wrong snipped, I've added the file and still no response.

  var cert = new Telegram.Bot.Types.FileToSend("certificate.pem", new FileStream(Server.MapPath("~/Resources/certificate.pem"), FileMode.Open));
      Telegram.Bot.Api bot = new Telegram.Bot.Api("mytoken");
      bot.SetWebhook("https://myhost:8443/api/webhook", cert).Wait();

I also bought a certificate from GoDaddy for 4 bucks to see if that was the problem and removed the cert parameter from the .SetWebhook method, but even with that it doesn't seem to work.

from telegram.bot.

MrRoundRobin avatar MrRoundRobin commented on May 18, 2024

Can you show me the actual handler?

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

Of course. The setup that I have is just an mvc application with an index page and an API controller.
This is my app_start:

void Application_Start(object sender, EventArgs e) {
      // Code that runs on application startup
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      // Setting up webhook
       var cert = new Telegram.Bot.Types.FileToSend("certificate.pem", new FileStream(Server.MapPath("~/Resources/certificate.pem"), FileMode.Open));
      Telegram.Bot.Api bot = new Telegram.Bot.Api("mytoken");
      bot.SetWebhook("https://myhost:8443/api/webhook", cert).Wait();
}

My controller with the post method which should receive the updates from telegram:

public class WebhookController: ApiController
    {
        #region Properties

        private Telegram.Bot.Api Bot { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Basic constructor for the api controller
        /// </summary>
        public WebhookController() : base()
        {
            Bot = new Telegram.Bot.Api(ConfigurationManager.AppSettings["mytoken"]);
        }

       #endregion

       public async Task<IHttpActionResult> Post(Update update) {
       var message = update.Message;

      Console.WriteLine("Received Message from {0}", message.Chat.Id);

       if (message.Type.Equals(MessageType.TextMessage)) {
        // Echo each Message
        await Bot.SendTextMessage(message.Chat.Id, message.Text);
       }
      else if (message.Type.Equals(MessageType.PhotoMessage)) {
        // Download Photo
        var file = await Bot.GetFile(message.Photo.LastOrDefault()?.FileId);

        var filename = file.FileId + "." + file.FilePath.Split('.').Last();

        using (var saveImageStream = System.IO.File.Open(filename, FileMode.Create)) {
          await file.FileStream.CopyToAsync(saveImageStream);
        }

        await Bot.SendTextMessage(message.Chat.Id, "Thx for the Pics");
      }

      return Ok();
    }
 }

from telegram.bot.

NeelBhatt avatar NeelBhatt commented on May 18, 2024

Might be too late to comment here but it is finally supported by .Net! Have a look here: https://neelbhatt40.wordpress.com/2015/10/14/webhooks-in-asp-net-a-visual-studio-extension/

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

That's very good news. Have you tried it yet, @NeelBhatt? I am excited to give it a go ASAP.

from telegram.bot.

NeelBhatt avatar NeelBhatt commented on May 18, 2024

Yes I have tried it. I will soon put some code in Github for demo project. @blacksagres

from telegram.bot.

m-shojaei avatar m-shojaei commented on May 18, 2024

@blacksagres did you managed to setup the webhook finally?

from telegram.bot.

blacksagres avatar blacksagres commented on May 18, 2024

@blacklabelmee Unfortunately no.

from telegram.bot.

m-shojaei avatar m-shojaei commented on May 18, 2024

@blacksagres thanks for your reply . i'm still trying if i succeeded will notify you :)

from telegram.bot.

HamidBaghernia avatar HamidBaghernia commented on May 18, 2024

There's a trick with the Certificates: You should create a plain text certificate chain if you're using a subdomain. The chain must be from the subdomain all the way to the root. I included three .pem files and concat them to a single file using Type command in cmd (type sub.domain.com.pem intermediate.pem root.pem >> bundle.pem). Make sure you delete any bundle.pem file before generating a new one.

Take a look at this:
https://core.telegram.org/bots/webhooks#setting-a-verified-webhook-with-an-untrusted-root

from telegram.bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.