Comments (19)
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.
I want webhook because im writing a webservice and i don want those get update and while... things..
from telegram.bot.
if you think that webservice that you are working on can help , can you send it to me?
from telegram.bot.
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.
I've created an example using OWIN and WebAPI, see 7ceaade
from telegram.bot.
@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.
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.
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.
bot.SetWebhook("https://myhost:8443/api/webhook").Wait();
must be
bot.SetWebhook("https://myhost:8443/api/webhook",cert).Wait();
from telegram.bot.
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.
Can you show me the actual handler?
from telegram.bot.
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.
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.
That's very good news. Have you tried it yet, @NeelBhatt? I am excited to give it a go ASAP.
from telegram.bot.
Yes I have tried it. I will soon put some code in Github for demo project. @blacksagres
from telegram.bot.
@blacksagres did you managed to setup the webhook finally?
from telegram.bot.
@blacklabelmee Unfortunately no.
from telegram.bot.
@blacksagres thanks for your reply . i'm still trying if i succeeded will notify you :)
from telegram.bot.
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)
- ChatMemberLeft works, but join event not works HOT 14
- updateHandler is not invoked asynchronously HOT 2
- Bot API 6.8 (August 18, 2023)
- Missing documentation InputOnlineFile => InputFileUrl HOT 3
- Bot API 6.9 (September 22, 2023)
- Could we have InputMediaBase inherit from IAlbumInputMedia? HOT 3
- Erro não autorizado HOT 2
- Bug Report - Lack of Graceful Shutdown in Message Updates Handling HOT 4
- Replace Newtonsoft.Json with System.Text.Json with proper source generation HOT 10
- Release 19.0.1 HOT 3
- Bot API 7.0 (December 29, 2023)
- Calling MakeRequestAsync with Stream content more then once results in error HOT 2
- Looks like this source generator is completely non incremental HOT 3
- telegram.bot is missing NuGet package README file
- Telegram.Bot.dll PUA classification with ESET problem HOT 5
- Send v20.0.0-alpha.3 to nuget plz HOT 1
- Project is not compatible with net8.0 (.NETCoreApp,Version=v8.0) HOT 7
- System.Text.Json deserialization issue HOT 5
- Temporary alternative NuGet feed due to AV software false positives
- GetStickerSetAsync() fails: Required property 'is_animated' not found in JSON HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telegram.bot.