Python

[Python] 카카오톡 오픈채팅방에 D-Day 알림봇 만들기

Always-Try 2022. 3. 20. 22:08
import gspread
import datetime
from oauth2client.service_account import ServiceAccountCredentials
import time, win32con, win32api, win32gui
import requests
from bs4 import BeautifulSoup
from apscheduler.schedulers.blocking import BlockingScheduler
import time, win32con, win32api, win32gui
from apscheduler.schedulers.background import BackgroundScheduler
import re
# # 카톡창 이름, (활성화 상태의 열려있는 창)
kakao_opentalk_name = '오픈채팅방이름'


# # 디데이 계산

def calculate_dday():
    today = datetime.date.today()
    test_day = datetime.date(2022,4,23)
    d_day = test_day - today
    message = "*D-day 알리미*\n정보보안기사 실기시험까지 {}일 남았습니다.".format(d_day.days)
    return message



# # 채팅방에 메시지 전송
def kakao_sendtext(chatroom_name, text):
    # # 핸들 _ 채팅방
    hwndMain = win32gui.FindWindow( None, chatroom_name)
    hwndEdit = win32gui.FindWindowEx( hwndMain, None, "RICHEDIT50W", None)

    win32api.SendMessage(hwndEdit, win32con.WM_SETTEXT, 0, text)
    SendReturn(hwndEdit)


# # 엔터
def SendReturn(hwnd):
    win32api.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    time.sleep(0.01)
    win32api.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)


# # 채팅방 열기

# # # 채팅방 목록 검색하는 Edit (채팅방이 열려있지 않아도 전송 가능하기 위하여)
def open_chatroom(chatroom_name):
    hwndkakao = win32gui.FindWindow(None, "카카오톡")
    hwndkakao_edit1 = win32gui.FindWindowEx( hwndkakao, None, "EVA_ChildWindow", None)
    hwndkakao_edit2_1 = win32gui.FindWindowEx( hwndkakao_edit1, None, "EVA_Window", None)
    hwndkakao_edit2_2 = win32gui.FindWindowEx( hwndkakao_edit1, hwndkakao_edit2_1, "EVA_Window", None)
    hwndkakao_edit3 = win32gui.FindWindowEx( hwndkakao_edit2_2, None, "Edit", None)

    # # Edit에 검색 _ 입력되어있는 텍스트가 있어도 덮어쓰기됨
    win32api.SendMessage(hwndkakao_edit3, win32con.WM_SETTEXT, 0, chatroom_name)
    time.sleep(1)   # 안정성 위해 필요
    SendReturn(hwndkakao_edit3)
    time.sleep(1)


def job_1():
    global old_links, count
    open_chatroom(kakao_opentalk_name)  # 채팅방 열기
    message = calculate_dday()
    kakao_sendtext(kakao_opentalk_name, message)


def main():
    global count
    sched = BackgroundScheduler()
    sched.start()
    #sched.add_job(job_1, 'cron', second='*/30', id="test_1")
    sched.add_job(job_1, 'cron', hour=9, id="test_1")
    while True:
        time.sleep(30)
        #print("Running wait")


if __name__ == '__main__':
    main()

기존 2021.10.16 - [Python] - 구글 스프레드 시트 데이터 카카오톡 오픈채팅으로 전송하기를 급하게 수정하다보니 모듈에 일부 필요없는 것들이 섞여 있긴하다. 적당히 걸러서 사용하기를..