-
Notifications
You must be signed in to change notification settings - Fork 675
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
Comments
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. |
I want webhook because im writing a webservice and i don want those get update and while... things.. |
if you think that webservice that you are working on can help , can you send it to me? |
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. |
I've created an example using OWIN and WebAPI, see 7ceaade |
@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.
If this information is relevant, my application is hosted on godaddy using a self-signed certificate. Many thanks in advance. |
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(); |
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. |
bot.SetWebhook("https://myhost:8443/api/webhook").Wait(); must be bot.SetWebhook("https://myhost:8443/api/webhook",cert).Wait(); |
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. |
Can you show me the actual handler? |
Of course. The setup that I have is just an mvc application with an index page and an API controller. 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();
}
} |
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/ |
That's very good news. Have you tried it yet, @NeelBhatt? I am excited to give it a go ASAP. |
Yes I have tried it. I will soon put some code in Github for demo project. @blacksagres |
@blacksagres did you managed to setup the webhook finally? |
@blacklabelmee Unfortunately no. |
@blacksagres thanks for your reply . i'm still trying if i succeeded will notify you :) |
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: |
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...
The text was updated successfully, but these errors were encountered: