|
30 | 30 | types.ReplyKeyboardRemove, types.ForceReply] |
31 | 31 |
|
32 | 32 |
|
| 33 | +# for webhooks |
| 34 | +from telebot.extensions import AsyncWebhookListener |
| 35 | +import string |
| 36 | +import random |
| 37 | +import ssl |
| 38 | + |
| 39 | + |
33 | 40 | """ |
34 | 41 | Module : telebot |
35 | 42 | """ |
@@ -1438,6 +1445,82 @@ async def set_webhook(self, url=None, certificate=None, max_connections=None, al |
1438 | 1445 | drop_pending_updates, timeout, secret_token) |
1439 | 1446 |
|
1440 | 1447 |
|
| 1448 | + async def run_webhooks(self, |
| 1449 | + listen: Optional[str]="127.0.0.1", |
| 1450 | + port: Optional[int]=443, |
| 1451 | + url_path: Optional[str]=None, |
| 1452 | + certificate: Optional[str]=None, |
| 1453 | + certificate_key: Optional[str]=None, |
| 1454 | + webhook_url: Optional[str]=None, |
| 1455 | + max_connections: Optional[int]=None, |
| 1456 | + allowed_updates: Optional[List]=None, |
| 1457 | + ip_address: Optional[str]=None, |
| 1458 | + drop_pending_updates: Optional[bool] = None, |
| 1459 | + timeout: Optional[int]=None, |
| 1460 | + secret_token: Optional[str]=None, |
| 1461 | + secret_token_length: Optional[int]=20, |
| 1462 | + debug: Optional[bool]=False): |
| 1463 | + """ |
| 1464 | + This class sets webhooks and listens to a given url and port. |
| 1465 | +
|
| 1466 | + :param listen: IP address to listen to. Defaults to |
| 1467 | + 0.0.0.0 |
| 1468 | + :param port: A port which will be used to listen to webhooks. |
| 1469 | + :param url_path: Path to the webhook. Defaults to /token |
| 1470 | + :param certificate: Path to the certificate file. |
| 1471 | + :param certificate_key: Path to the certificate key file. |
| 1472 | + :param webhook_url: Webhook URL. |
| 1473 | + :param max_connections: Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput. |
| 1474 | + :param allowed_updates: A JSON-serialized list of the update types you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used. |
| 1475 | + :param ip_address: The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS |
| 1476 | + :param drop_pending_updates: Pass True to drop all pending updates |
| 1477 | + :param timeout: Integer. Request connection timeout |
| 1478 | + :param secret_token: Secret token to be used to verify the webhook request. |
| 1479 | + :return: |
| 1480 | + """ |
| 1481 | + |
| 1482 | + # generate secret token if not set |
| 1483 | + if not secret_token: |
| 1484 | + secret_token = ''.join(random.choices(string.ascii_uppercase + string.digits, k=secret_token_length)) |
| 1485 | + |
| 1486 | + |
| 1487 | + if not url_path: |
| 1488 | + url_path = self.token + '/' |
| 1489 | + if url_path[-1] != '/': url_path += '/' |
| 1490 | + |
| 1491 | + |
| 1492 | + |
| 1493 | + protocol = "https" if certificate else "http" |
| 1494 | + if not webhook_url: |
| 1495 | + webhook_url = "{}://{}:{}/{}".format(protocol, listen, port, url_path) |
| 1496 | + |
| 1497 | + if certificate and certificate_key: |
| 1498 | + ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) |
| 1499 | + ssl_ctx.load_cert_chain(certificate, certificate_key) |
| 1500 | + else: |
| 1501 | + ssl_ctx = None |
| 1502 | + |
| 1503 | + # open certificate if it exists |
| 1504 | + cert_file = open(certificate, 'rb') if certificate else None |
| 1505 | + await self.set_webhook( |
| 1506 | + url=webhook_url, |
| 1507 | + certificate=cert_file, |
| 1508 | + max_connections=max_connections, |
| 1509 | + allowed_updates=allowed_updates, |
| 1510 | + ip_address=ip_address, |
| 1511 | + drop_pending_updates=drop_pending_updates, |
| 1512 | + timeout=timeout, |
| 1513 | + secret_token=secret_token |
| 1514 | + ) |
| 1515 | + if cert_file: cert_file.close() |
| 1516 | + |
| 1517 | + ssl_context = (certificate, certificate_key) if certificate else (None, None) |
| 1518 | + self.webhook_listener = AsyncWebhookListener(self, secret_token, listen, port, ssl_context, '/'+url_path, debug) |
| 1519 | + # create a new loop, set it, and pass it |
| 1520 | + asyncio.set_event_loop(asyncio.new_event_loop()) |
| 1521 | + await self.webhook_listener.run_app() |
| 1522 | + |
| 1523 | + |
1441 | 1524 |
|
1442 | 1525 | async def delete_webhook(self, drop_pending_updates=None, timeout=None): |
1443 | 1526 | """ |
|
0 commit comments