From 67b02a3768719d0583bad24181205c3eeeea4cc1 Mon Sep 17 00:00:00 2001 From: Tamar Kalir Date: Tue, 20 Dec 2022 14:44:02 +0200 Subject: [PATCH 1/3] commit --- anyway/models.py | 7 +++ anyway/parsers/news_flash_db_adapter.py | 1 + anyway/telegram_accident_notifications.py | 52 +++++++++++++++++++++++ main.py | 12 +++++- requirements.txt | 3 +- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 anyway/telegram_accident_notifications.py diff --git a/anyway/models.py b/anyway/models.py index f71d8554b..22110d4e7 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 @@ -2769,3 +2770,9 @@ 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()) + test_or_prod = Column(Enum("TEST", "PROD", name="test_prod_enum", create_type=True)) \ 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 From 374765763208b458cc9724d977ae4963f5b39e3f Mon Sep 17 00:00:00 2001 From: Tamar Kalir Date: Fri, 20 Jan 2023 18:25:42 +0200 Subject: [PATCH 2/3] adding TelegramGroups & TelegramGroupsTest to models.py --- anyway/models.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/anyway/models.py b/anyway/models.py index 44ec845b7..11709725f 100755 --- a/anyway/models.py +++ b/anyway/models.py @@ -39,7 +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 sqlalchemy.dialects.postgresql import Enum, JSON from anyway import localization from anyway.backend_constants import BE_CONST, NewsflashLocationQualification @@ -2802,7 +2802,11 @@ class CBSLocations(Base): latitude = Column(Float(), nullable=True) class TelegramGroups(Base): - __tablename__ = "telegram_groups" + __tablename__ = "telegram_groups" + id = Column(Integer(), primary_key=True) + filter = Column(JSON()) + +class TelegramGroupsTest(Base): + __tablename__ = "telegram_groups_test" id = Column(Integer(), primary_key=True) - filter = Column(JSON()) - test_or_prod = Column(Enum("TEST", "PROD", name="test_prod_enum", create_type=True)) \ No newline at end of file + filter = Column(JSON()) \ No newline at end of file From c15af77559a57c34478af2ef958e96666704ee96 Mon Sep 17 00:00:00 2001 From: Tamar Kalir Date: Thu, 9 Feb 2023 16:23:53 +0200 Subject: [PATCH 3/3] add defaults to TelegramGroups table --- anyway/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/anyway/models.py b/anyway/models.py index 11709725f..afb92ad51 100755 --- a/anyway/models.py +++ b/anyway/models.py @@ -2804,9 +2804,9 @@ class CBSLocations(Base): class TelegramGroups(Base): __tablename__ = "telegram_groups" id = Column(Integer(), primary_key=True) - filter = Column(JSON()) + filter = Column(JSON(), server_default="{}") class TelegramGroupsTest(Base): __tablename__ = "telegram_groups_test" id = Column(Integer(), primary_key=True) - filter = Column(JSON()) \ No newline at end of file + filter = Column(JSON(), server_default="{}") \ No newline at end of file