Differences
This shows you the differences between two versions of the page.
python:python_programs:rswqueuebot_takecontrolmessenger [2024/05/07 04:47] – created jcorona | python:python_programs:rswqueuebot_takecontrolmessenger [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~cssp_main_openDIV~~ | ||
- | ====== RSWQueueBot Take Control Messenger | ||
- | |||
- | |||
- | This bot built in python will listen for new events added to the **email_event** table and then send them to the **Trillian** support chat. \\ | ||
- | Once an **event** is processed, the processed field in SQL will be updated to 1, ensuring that the event is not reprocessed again. | ||
- | |||
- | The objective is to notify everyone that there is a new live chat incoming, no matter what it is. | ||
- | |||
- | <code python> | ||
- | # -*- coding: utf-8 -*- | ||
- | """ | ||
- | Created on Sun May 5 07:28:50 2024 | ||
- | |||
- | @author: jcorona | ||
- | |||
- | http:// | ||
- | |||
- | Required Sister Application: | ||
- | http:// | ||
- | |||
- | Required Database Name: | ||
- | RSWQueueBotDB | ||
- | |||
- | CREATE DATABASE RSWQueueBotDB; | ||
- | |||
- | |||
- | Required Tables: | ||
- | | ||
- | CREATE TABLE Email_Event ( | ||
- | ID INT IDENTITY(1, | ||
- | Subject NVARCHAR(255), | ||
- | Body NVARCHAR(MAX) | ||
- | ); | ||
- | ----- | ||
- | |||
- | ALTER TABLE Email_Event | ||
- | ADD processed BIT DEFAULT 0 NOT NULL; | ||
- | |||
- | |||
- | """ | ||
- | import pyodbc | ||
- | import time | ||
- | import re | ||
- | from pywinauto.application import Application | ||
- | from pynput.keyboard import Controller, Key | ||
- | |||
- | def connect_to_database(): | ||
- | conn = pyodbc.connect(' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | return conn | ||
- | |||
- | def fetch_emails(cursor): | ||
- | cursor.execute(" | ||
- | emails = cursor.fetchall() | ||
- | cleaned_emails = [] | ||
- | for email in emails: | ||
- | email_id, subject, body = email | ||
- | # Clean the subject from new lines and other unwanted characters | ||
- | cleaned_subject = re.sub(r' | ||
- | cleaned_emails.append((email_id, | ||
- | return cleaned_emails | ||
- | |||
- | def extract_description(email_body): | ||
- | start = " | ||
- | end = " | ||
- | try: | ||
- | description = email_body.split(start)[1].split(end)[0].strip() | ||
- | return description if description else " | ||
- | except IndexError: | ||
- | return " | ||
- | |||
- | def update_processed_status(cursor, | ||
- | cursor.execute(" | ||
- | cursor.commit() | ||
- | |||
- | def main(): | ||
- | conn = connect_to_database() | ||
- | cursor = conn.cursor() | ||
- | keyboard = Controller() | ||
- | app = Application().connect(title=" | ||
- | main_window = app.window(title=' | ||
- | main_window.set_focus() | ||
- | main_window.type_keys(' | ||
- | keyboard.press(Key.enter) | ||
- | keyboard.release(Key.enter) | ||
- | |||
- | while True: | ||
- | emails = fetch_emails(cursor) | ||
- | for email in emails: | ||
- | email_id, subject, body = email | ||
- | description = extract_description(body) | ||
- | subject = subject if subject else " | ||
- | description = description if description else " | ||
- | # Automation to enter data into Trillian | ||
- | support_chat = app.window(title=' | ||
- | support_chat.set_focus() | ||
- | message = f" | ||
- | support_chat.type_keys(message, | ||
- | time.sleep(2) | ||
- | keyboard.press(Key.enter) | ||
- | keyboard.release(Key.enter) | ||
- | keyboard.press(Key.enter) | ||
- | keyboard.release(Key.enter) | ||
- | update_processed_status(cursor, | ||
- | |||
- | time.sleep(5) | ||
- | |||
- | if __name__ == ' | ||
- | main() | ||
- | |||
- | </ | ||
- | |||
- | ~~cssp_closeDIV~~ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ | ||
- | \\ |