Source code for airflow.providers.telegram.operators.telegram
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Operator for Telegram."""from__future__importannotationsfromtypingimportTYPE_CHECKING,Sequencefromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.telegram.hooks.telegramimportTelegramHookifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classTelegramOperator(BaseOperator):""" This operator allows you to post messages to Telegram using Telegram Bot API. Takes both Telegram Bot API token directly or connection that has Telegram token in password field. If both supplied, token parameter will be given precedence. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:TelegramOperator` :param telegram_conn_id: Telegram connection ID which its password is Telegram API token :param token: Telegram API Token :param chat_id: Telegram chat ID for a chat/channel/group :param text: Message to be sent on telegram :param telegram_kwargs: Extra args to be passed to telegram client """
def__init__(self,*,telegram_conn_id:str="telegram_default",token:str|None=None,chat_id:str|None=None,text:str="No message has been set.",telegram_kwargs:dict|None=None,**kwargs,):self.chat_id=chat_idself.token=tokenself.telegram_kwargs=telegram_kwargsor{}self.text=textiftelegram_conn_idisNone:raiseAirflowException("No valid Telegram connection id supplied.")self.telegram_conn_id=telegram_conn_idsuper().__init__(**kwargs)
[docs]defexecute(self,context:Context)->None:"""Call the TelegramHook to post the provided Telegram message."""ifself.text:self.telegram_kwargs["text"]=self.texttelegram_hook=TelegramHook(telegram_conn_id=self.telegram_conn_id,token=self.token,chat_id=self.chat_id,)telegram_hook.send_message(self.telegram_kwargs)