Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set webhook example? #39

Closed
Navid-Sa opened this issue Sep 21, 2015 · 19 comments
Closed

set webhook example? #39

Navid-Sa opened this issue Sep 21, 2015 · 19 comments
Labels
💗 help wanted Up for grabs. We would accept a PR to help resolve this issue

Comments

@Navid-Sa
Copy link

Hi,

I'm very new in this telegram things.
can you help me and make an example for using webhook and certificate parameter?

I just don no how to use webhook in my C# program,
before this certificate parameter I was working with getupdate.

Thanks...

@MrRoundRobin
Copy link
Member

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.

@Navid-Sa
Copy link
Author

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

@Navid-Sa
Copy link
Author

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

@ScottRFrost
Copy link

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.

@MrRoundRobin
Copy link
Member

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

@MrRoundRobin MrRoundRobin added the 💗 help wanted Up for grabs. We would accept a PR to help resolve this issue label Sep 24, 2015
@blacksagres
Copy link

@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.

@MrRoundRobin
Copy link
Member

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

@blacksagres
Copy link

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.

@MrRoundRobin
Copy link
Member

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

must be

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

@blacksagres
Copy link

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.

@MrRoundRobin
Copy link
Member

Can you show me the actual handler?

@blacksagres
Copy link

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

@NeelBhatt
Copy link

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/

@blacksagres
Copy link

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

@NeelBhatt
Copy link

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

@m-shojaei
Copy link

@blacksagres did you managed to setup the webhook finally?

@blacksagres
Copy link

@blacklabelmee Unfortunately no.

@m-shojaei
Copy link

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

@HamidBaghernia
Copy link

HamidBaghernia commented Aug 30, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💗 help wanted Up for grabs. We would accept a PR to help resolve this issue
Projects
None yet
Development

No branches or pull requests

7 participants