DinoLeung / TeleDart

A Dart library interfacing with the latest Telegram Bot API.
https://pub.dev/packages/teledart
GNU General Public License v3.0
314 stars 69 forks source link

how to web hook create bot pls example file path and certificate #273

Open abduraimovdev opened 3 months ago

abduraimovdev commented 3 months ago

how to web hook create bot pls example file path and certificate

HeySreelal commented 3 months ago

Hey, I've checked the example file, and I think that's a bit outdated.

Here's an example that I just tested and found to be working fine:

import 'dart:io';
import 'package:teledart/teledart.dart';
import 'package:teledart/telegram.dart';

void main() async {
  final token = Platform.environment["BOT_TOKEN"]!;
  final telegram = Telegram(token);
  final username = (await telegram.getMe()).username!;

  final server = await HttpServer.bind(InternetAddress.anyIPv4, 8081);

  final webhook = Webhook(
    telegram,
    "https://yourwebhook-url.com",
    server,
  );

  final bot = TeleDart(
    token,
    Event(username),
    fetcher: webhook,
  );

  bot.onCommand("start").listen((message) {
    message.reply("Hello!");
  });

  bot.start();
}

If you have a self-signed certificate you can pass the File object to the certificate parameter - it's optional. Check the setWebhook documentation for more info. Anyway, the above code is working just fine.

Let me know if you have any further concerns.

DinoLeung commented 3 months ago

Thanks @HeySreelal for always being the first person to help in the community. I'm working on an update for a while which will include an updated version of the example file but I've not been able to finish it still. @abduraimovdev please consult the official faq for the webhook configuration https://core.telegram.org/bots/faq#i-39m-having-problems-with-webhooks

paulobreim commented 3 months ago

In my Bots the code I use is the one below.

What is this webhook? When is it used?

void main() async {
   late TeleDart teledart;
   const token = mytoken;

  final username = (await Telegram(token).getMe()).username;
  teledart = TeleDart(token, Event(username!));
  print('$username ready to work.');
  teledart.start();

  teledart.onMessage(entityType: 'bot_command').listen((message) async {
    checkmessage(message);  // there answer everything
  });

}
HeySreelal commented 3 months ago

Hi @paulobreim, Telegram supports two ways in which you can listen updates. First one is the LongPolling which is used by default if you don't pass a fetcher. So from your code, I can say that your bots are using the LongPolling way to fetch updates.


Some side notes are, generally speaking (from my understanding) webhooks are preferred for a bot with large user base. Because, an update is immediately sent to your endpoint right after it is occurred also as they are concurrently processed.

Hope this helps :)

paulobreim commented 3 months ago

Glad to know that. I didn't know that Telegram had a webhook, I imagine it must be very useful in a payment system, which we still don't have in Brazil.

tks paulo