From 6b4bd59e4f3c727ba98ada5b24ef7501a04ad07b Mon Sep 17 00:00:00 2001 From: tkalir Date: Wed, 6 Dec 2023 19:50:17 +0200 Subject: [PATCH] adding telegram endpoint for webhook and table for tracking forwarded messages --- .../881e7b1dba8a_track_telegram_messages.py | 32 +++++++++++++++++++ anyway/flask_app.py | 8 +++++ anyway/models.py | 6 ++++ 3 files changed, 46 insertions(+) create mode 100644 alembic/versions/881e7b1dba8a_track_telegram_messages.py diff --git a/alembic/versions/881e7b1dba8a_track_telegram_messages.py b/alembic/versions/881e7b1dba8a_track_telegram_messages.py new file mode 100644 index 00000000..4b23bd0c --- /dev/null +++ b/alembic/versions/881e7b1dba8a_track_telegram_messages.py @@ -0,0 +1,32 @@ +"""track telegram messages + +Revision ID: 881e7b1dba8a +Revises: 664f8a93794e +Create Date: 2023-11-21 12:39:32.931262 + +""" + +# revision identifiers, used by Alembic. +revision = '881e7b1dba8a' +down_revision = '664f8a93794e' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('telegram_forwarded_messages', + sa.Column('message_id', sa.String(), nullable=True), + sa.Column('newsflash_id', sa.Integer(), nullable=False), + sa.Column('group_sent', sa.String(), nullable=False), + sa.PrimaryKeyConstraint('message_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('telegram_forwarded_messages') + # ### end Alembic commands ### diff --git a/anyway/flask_app.py b/anyway/flask_app.py index d3e1fc82..534a848b 100755 --- a/anyway/flask_app.py +++ b/anyway/flask_app.py @@ -1335,6 +1335,14 @@ def embedded_reports_api(): return response +@app.route("/api/telegram/webhook", methods=["POST"]) +def telegram_webhook(): + update = request.json # Telegram sends updates in JSON format + logging.info(f"Received Telegram update: {update}") + + return jsonify(success=True) + + # User system API app.add_url_rule("/user/add_role", view_func=add_role, methods=["POST"]) app.add_url_rule( diff --git a/anyway/models.py b/anyway/models.py index 54c51b82..8943cf2d 100755 --- a/anyway/models.py +++ b/anyway/models.py @@ -2975,3 +2975,9 @@ class TelegramGroups(TelegramGroupsBase): class TelegramGroupsTest(TelegramGroupsBase): __tablename__ = "telegram_groups_test" + +class TelegramForwardedMessages(): + __tablename__ = 'telegram_forwarded_messages' + message_id = Column(String(), primary_key=True) + newsflash_id = Column(BigInteger(), nullable=False) + group_sent = Column(String(), nullable=False),