Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 2321 creating telegram push notifications #2334

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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="{}")
1 change: 1 addition & 0 deletions anyway/parsers/news_flash_db_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions anyway/telegram_accident_notifications.py
Original file line number Diff line number Diff line change
@@ -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")
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
swifter==1.3.4
python-telegram-bot