-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #637 from UnitapApp/feature/telegram/implementation
Feature/telegram/implementation
- Loading branch information
Showing
33 changed files
with
1,199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import logging | ||
|
||
from core.constraints.abstract import ( | ||
ConstraintApp, | ||
ConstraintVerification, | ||
) | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HasTelegramConnection(ConstraintVerification): | ||
_param_keys = [] | ||
app_name = ConstraintApp.GENERAL.value | ||
|
||
def is_observed(self, *args, **kwargs) -> bool: | ||
from telegram.models import TelegramConnection | ||
|
||
try: | ||
twitter = TelegramConnection.get_connection(self.user_profile) | ||
except TelegramConnection.DoesNotExist: | ||
return False | ||
return twitter.is_connected() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.conf import settings | ||
|
||
import hashlib | ||
import hmac | ||
import time | ||
|
||
|
||
def verify_telegram_auth(bot_token, data): | ||
auth_data = dict(data) | ||
hash_check = auth_data.pop("hash") | ||
|
||
# Create the data string by sorting keys and concatenating key=value pairs | ||
data_check_string = "\n".join([f"{k}={v}" for k, v in sorted(auth_data.items())]) | ||
|
||
# Hash the data string with your bot's token | ||
secret_key = hashlib.sha256(bot_token.encode()).digest() | ||
calculated_hash = hmac.new( | ||
secret_key, data_check_string.encode(), hashlib.sha256 | ||
).hexdigest() | ||
|
||
# Compare the calculated hash with the received hash | ||
if calculated_hash != hash_check: | ||
return False | ||
|
||
return time.time() - int(auth_data["auth_date"]) <= 86400 | ||
|
||
|
||
class TelegramUtil: | ||
bot_token = settings.TELEGRAM_BOT_API_KEY | ||
bot_username = settings.TELEGRAM_BOT_USERNAME | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def verify_login(self, telegram_data): | ||
return verify_telegram_auth(self.bot_token, telegram_data) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Generated by Django 5.1.2 on 2024-10-17 11:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def create_prizetap_constraint(apps, schema_editor): | ||
Constraint = apps.get_model("prizetap", "Constraint") | ||
|
||
Constraint.objects.create( | ||
name="core.HasTelegramConnection", | ||
description="HasTelegramConnection", | ||
title="Connect Telegram", | ||
type="VER", | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("prizetap", "0080_alter_constraint_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="constraint", | ||
name="name", | ||
field=models.CharField( | ||
choices=[ | ||
("core.BrightIDMeetVerification", "BrightIDMeetVerification"), | ||
("core.BrightIDAuraVerification", "BrightIDAuraVerification"), | ||
("core.HasNFTVerification", "HasNFTVerification"), | ||
("core.HasTokenVerification", "HasTokenVerification"), | ||
( | ||
"core.HasTokenTransferVerification", | ||
"HasTokenTransferVerification", | ||
), | ||
("core.AllowListVerification", "AllowListVerification"), | ||
("core.HasENSVerification", "HasENSVerification"), | ||
("core.HasLensProfile", "HasLensProfile"), | ||
("core.IsFollowingLensUser", "IsFollowingLensUser"), | ||
("core.BeFollowedByLensUser", "BeFollowedByLensUser"), | ||
("core.DidMirrorOnLensPublication", "DidMirrorOnLensPublication"), | ||
("core.DidCollectLensPublication", "DidCollectLensPublication"), | ||
("core.HasMinimumLensPost", "HasMinimumLensPost"), | ||
("core.HasMinimumLensFollower", "HasMinimumLensFollower"), | ||
("core.BeFollowedByFarcasterUser", "BeFollowedByFarcasterUser"), | ||
("core.HasMinimumFarcasterFollower", "HasMinimumFarcasterFollower"), | ||
("core.DidLikedFarcasterCast", "DidLikedFarcasterCast"), | ||
("core.DidRecastFarcasterCast", "DidRecastFarcasterCast"), | ||
("core.IsFollowingFarcasterUser", "IsFollowingFarcasterUser"), | ||
("core.HasFarcasterProfile", "HasFarcasterProfile"), | ||
("core.BeAttestedBy", "BeAttestedBy"), | ||
("core.Attest", "Attest"), | ||
("core.HasDonatedOnGitcoin", "HasDonatedOnGitcoin"), | ||
("core.HasMinimumHumanityScore", "HasMinimumHumanityScore"), | ||
("core.HasGitcoinPassportProfile", "HasGitcoinPassportProfile"), | ||
("core.IsFollowingFarcasterChannel", "IsFollowingFarcasterChannel"), | ||
("core.BridgeEthToArb", "BridgeEthToArb"), | ||
("core.IsFollowingTwitterUser", "IsFollowingTwitterUser"), | ||
("core.BeFollowedByTwitterUser", "BeFollowedByTwitterUser"), | ||
("core.DidRetweetTweet", "DidRetweetTweet"), | ||
("core.DidQuoteTweet", "DidQuoteTweet"), | ||
("core.HasMuonNode", "HasMuonNode"), | ||
("core.DelegateArb", "DelegateArb"), | ||
("core.DelegateOP", "DelegateOP"), | ||
("core.DidDelegateArbToAddress", "DidDelegateArbToAddress"), | ||
("core.DidDelegateOPToAddress", "DidDelegateOPToAddress"), | ||
("core.GLMStakingVerification", "GLMStakingVerification"), | ||
("core.IsFollowingTwitterBatch", "IsFollowingTwitterBatch"), | ||
("core.IsFollowingFarcasterBatch", "IsFollowingFarcasterBatch"), | ||
( | ||
"core.HasVerifiedCloudflareCaptcha", | ||
"HasVerifiedCloudflareCaptcha", | ||
), | ||
("core.DidMintZoraNFT", "DidMintZoraNFT"), | ||
("core.HasVerifiedHCaptcha", "HasVerifiedHCaptcha"), | ||
("core.HasTelegramConnection", "HasTelegramConnection"), | ||
("prizetap.HaveUnitapPass", "HaveUnitapPass"), | ||
("prizetap.NotHaveUnitapPass", "NotHaveUnitapPass"), | ||
("faucet.OptimismDonationConstraint", "OptimismDonationConstraint"), | ||
( | ||
"faucet.OptimismClaimingGasConstraint", | ||
"OptimismClaimingGasConstraint", | ||
), | ||
], | ||
max_length=255, | ||
unique=True, | ||
), | ||
), | ||
migrations.RunPython( | ||
create_prizetap_constraint, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# admin.py | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.shortcuts import render | ||
from .models import TelegramConnection | ||
from .forms import BroadcastMessageForm | ||
from telegram.bot import TelegramMessenger | ||
from django.core.exceptions import PermissionDenied | ||
|
||
|
||
@admin.register(TelegramConnection) | ||
class TelegramConnectionAdmin(admin.ModelAdmin): | ||
list_display = ("pk", "user_profile", "user_id") # Adjust as per your model fields | ||
|
||
def get_urls(self): | ||
urls = super().get_urls() | ||
custom_urls = [ | ||
path( | ||
"broadcast/", | ||
self.admin_site.admin_view(self.broadcast_view), | ||
name="broadcast_message", | ||
), | ||
] | ||
return custom_urls + urls | ||
|
||
@admin.action( | ||
description="Broadcast message to all users", | ||
permissions=["telegram.can_broadcast"], | ||
) | ||
def broadcast_view(self, request): | ||
if not request.user.has_perm("telegram.can_broadcast"): | ||
raise PermissionDenied("You do not have permission to broadcast messages.") | ||
|
||
if request.method == "POST": | ||
form = BroadcastMessageForm(request.POST) | ||
if form.is_valid(): | ||
message = form.cleaned_data["message"] | ||
users = TelegramConnection.objects.all() | ||
messenger = TelegramMessenger.get_instance() | ||
for user in users: | ||
messenger.send_message(user.user_id, text=message) | ||
|
||
self.message_user(request, "Message sent to all users!") | ||
return render(request, "admin/broadcast.html", {"form": form}) | ||
else: | ||
form = BroadcastMessageForm() | ||
|
||
return render(request, "admin/broadcast.html", {"form": form}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.apps import AppConfig | ||
from django.conf import settings | ||
|
||
|
||
class TelegramConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "telegram" | ||
|
||
def ready(self) -> None: | ||
if settings.DEPLOYMENT_ENV == "DEV": | ||
return super().ready() | ||
|
||
from .bot import TelegramMessenger | ||
from telegram import messages | ||
|
||
messenger = TelegramMessenger.get_instance() | ||
messenger.ensure_webhook() | ||
messenger.ready() | ||
|
||
return super().ready() |
Oops, something went wrong.