diff --git a/anyway/models.py b/anyway/models.py index 1e7115e93..afb92ad51 100755 --- a/anyway/models.py +++ b/anyway/models.py @@ -39,6 +39,7 @@ class UserMixin: import sqlalchemy from sqlalchemy.orm import relationship, load_only, backref from sqlalchemy import or_, and_ +#from sqlalchemy.dialects.postgresql import Enum, JSON from anyway import localization from anyway.backend_constants import BE_CONST, NewsflashLocationQualification @@ -2799,3 +2800,13 @@ class CBSLocations(Base): road_segment_name = Column(Text(), nullable=True) longitude = Column(Float(), nullable=True) latitude = Column(Float(), nullable=True) + +class TelegramGroups(Base): + __tablename__ = "telegram_groups" + id = Column(Integer(), primary_key=True) + filter = Column(JSON(), server_default="{}") + +class TelegramGroupsTest(Base): + __tablename__ = "telegram_groups_test" + id = Column(Integer(), primary_key=True) + filter = Column(JSON(), server_default="{}") \ No newline at end of file diff --git a/anyway/parsers/news_flash_db_adapter.py b/anyway/parsers/news_flash_db_adapter.py index e2d5889ce..7c01131ef 100644 --- a/anyway/parsers/news_flash_db_adapter.py +++ b/anyway/parsers/news_flash_db_adapter.py @@ -84,6 +84,7 @@ def insert_new_newsflash(self, newsflash: NewsFlash) -> None: infographics_data_cache_updater.add_news_flash_to_cache(newsflash) if os.environ.get("FLASK_ENV") == "production" and newsflash.accident: publish_notification(newsflash) + publish_notification(newsflash) def get_newsflash_by_id(self, id): return self.db.session.query(NewsFlash).filter(NewsFlash.id == id) diff --git a/anyway/telegram_accident_notifications.py b/anyway/telegram_accident_notifications.py new file mode 100644 index 000000000..df41b3423 --- /dev/null +++ b/anyway/telegram_accident_notifications.py @@ -0,0 +1,52 @@ +from dataclasses import dataclass, asdict +from typing import List + +import requests +from anyway import secrets +import telegram +from telegram import ParseMode +from anyway.models import NewsFlash +from anyway.parsers.infographics_data_cache_updater import is_in_cache + +INFOGRAPHIC_URL = "https://media.anyway.co.il/newsflash/" + +def publish_notification(newsflash: NewsFlash): + from anyway.slack_accident_notifications import fmt_lnk_mrkdwn + + bot_token = "5826636904:AAFy6lIfzYNA8IB8zZeUo7WhtLAyude4tis" + bot_user_name = "boty" + TOKEN = bot_token + bot = telegram.Bot(token=TOKEN) + #notification = gen_notification(newsflash) + #requests.post(secrets.get("SLACK_WEBHOOK_URL"), json=asdict(notification)) + chat_id = -790458867 #5826636904 + newsflash_id = 14680 #newsflash.id + msg_text = fmt_lnk_mrkdwn(f"{INFOGRAPHIC_URL}{newsflash_id}", "infographic") + bot.sendMessage(chat_id=chat_id, text=msg_text, parse_mode=ParseMode.MARKDOWN_V2) + +bot_token = "5826636904:AAFy6lIfzYNA8IB8zZeUo7WhtLAyude4tis" +bot_user_name = "boty" + +# import everything +#from flask import Flask, request +#import telegram +#from telebot.credentials import bot_token, bot_user_name,URL +# global bot +# global TOKEN +TOKEN = bot_token +bot = telegram.Bot(token=TOKEN) + +def respond(): + # retrieve the message in JSON and then transform it to Telegram object + #update = telegram.Update.de_json("miao", bot) + + #chat_id = update.message.chat.id + chat_id = 5826636904 + msg_id = update.message.message_id + + # Telegram understands UTF-8, so encode text for unicode compatibility + text = update.message.text.encode('utf-8').decode() + # for debugging purposes only + print("got text message :", text) + # the first time you chat with the bot AKA the welcoming message + bot.sendMessage(chat_id=chat_id, text="miao") \ No newline at end of file diff --git a/main.py b/main.py index 2110fdf74..a7fe77c92 100755 --- a/main.py +++ b/main.py @@ -438,6 +438,16 @@ def importemail(): return main() +@cli.group() +def send(): + pass + +@send.command() +def send_message(): + from anyway.telegram_accident_notifications import publish_notification + + publish_notification(None) + if __name__ == "__main__": - cli(sys.argv[1:]) # pylint: disable=too-many-function-args + cli(sys.argv[1:]) # pylint: disable=too-many-function-args \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ff5eadc50..b9a7b1c6a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -49,4 +49,5 @@ py3-validate-email==0.2.16 phonenumbers==8.12.21 flask-principal==0.4.0 pydantic==1.8.2 -swifter==1.3.4 \ No newline at end of file +swifter==1.3.4 +python-telegram-bot \ No newline at end of file