Created
May 3, 2022 22:43
-
-
Save parksunwoo/e66945fd13cc4fdf05fc0fcbd5236789 to your computer and use it in GitHub Desktop.
ms_teams_webhook_notification_with_table_python
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
import os | |
from datetime import timedelta | |
import logging | |
import json | |
import urllib3 | |
from django.core.management.base import BaseCommand | |
from django.utils import timezone | |
from core.models import NodeProgrs, NodeSchedule, User | |
from core.utils import time_converter | |
SUMMARY_DAYS = os.environ.get("SUMMARY_DAYS", "7") | |
RUNNING_SCHOOL = [1, 2, 3, 4, 5] | |
http = urllib3.PoolManager() | |
class Command(BaseCommand): | |
help = "ms_teams_webhook_noti_with_table_python" | |
def handle(self, *args, **kwargs): | |
if "TEAMS_02_NETWROK_WEBHOOK" in os.environ: | |
TEAMS_02_NETWROK_WEBHOOK = os.environ["TEAMS_02_NETWROK_WEBHOOK"] | |
today = timezone.now().strftime("%Y년 %m월 %d일") | |
warn_user = ( | |
User.objects.filter( | |
is_active=True, | |
is_student=True, | |
is_coach=False, | |
is_editor=False, | |
is_staff=False, | |
local_school__in=RUNNING_SCHOOL, | |
last_login__lte=timezone.now() - timedelta(days=int(SUMMARY_DAYS)), | |
) | |
.order_by("local_school", "last_name") | |
) | |
count_t = str(warn_user.count()) | |
txt_msg = ( | |
"__________________________________________________________________" | |
) | |
txt_msg += "\r\n\r\n" | |
txt_msg += "{:10s} / {:18s} / {:20s} / {:10s}".format( | |
"캠퍼스", "이름", "username", "마지막로그인" | |
) | |
txt_msg += "\r\n\r\n" | |
txt_msg += ( | |
"__________________________________________________________________" | |
) | |
for u in warn_user: | |
user_school = u.local_school.all().last().title.split(" ")[1] | |
txt_msg += "\r\n\r\n" | |
txt_msg += "{: <10s} / {: <18s} / {: <20s} / {: <10s}".format( | |
user_school, | |
u.last_name + u.first_name, | |
u.username[:20], | |
time_converter(u.last_login, fmt="%y.%m.%d"), | |
) | |
msg = { | |
"type": "message", | |
"summary": "my summary", | |
"attachments": [ | |
{ | |
"contentType": "application/vnd.microsoft.card.adaptive", | |
"contentUrl": None, | |
"content": { | |
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json", | |
"type": "AdaptiveCard", | |
"version": "1.2", | |
"body": [ | |
{ | |
"type": "Container", | |
"id": "fbcee869-2754-287d-bb37-145a4ccd750b", | |
"padding": "Default", | |
"spacing": "None", | |
"items": [ | |
{ | |
"type": "TextBlock", | |
"id": "44906797-222f-9fe2-0b7a-e3ee21c6e380", | |
"text": "[" | |
+ today | |
+ "] Inactive User Notification (" | |
+ count_t | |
+ "명)", | |
"wrap": True, | |
"weight": "Bolder", | |
"size": "Large", | |
}, | |
{ | |
"type": "TextBlock", | |
"id": "f7abdf1a-3cce-2159-28ef-f2f362ec937e", | |
"text": txt_msg, | |
"wrap": True, | |
}, | |
], | |
} | |
], | |
}, | |
} | |
], | |
} | |
encoded_msg = json.dumps(msg).encode("utf-8") | |
resp = http.request("POST", TEAMS_02_NETWROK_WEBHOOK, body=encoded_msg) | |
print("#" * 80) | |
print({"status_code": resp.status, "response": resp.data}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment