1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで勤怠システムを自動化

Posted at

Pythonで勤怠システムを自動化:SeleniumとDiscordボットの活用

はじめに

この記事では、Pythonを使用して勤怠操作を自動化するスクリプトを紹介します。SeleniumとDiscordボットを組み合わせることで、出勤と退勤の操作をリモートで簡単に行えるようになります。

機能概要

  • Seleniumを使用したWeb操作の自動化
  • Discordボットを介したリモート操作
  • 出勤・退勤操作の自動化
  • エラー時の自動リトライ機能

必要な環境

  • Python 3.7以上
  • Selenium
  • Discord.py
  • ChromeDriver(Seleniumで使用)

ライブラリのインストール

以下のコマンドで必要なライブラリをインストールしてください。

pip install discord selenium

コードの説明

このスクリプトは主に以下の部分から構成されています:

  1. 認証情報の取得
  2. Seleniumを使用したWeb操作
  3. Discordボットの設定と操作

それでは、具体的なコードを見ていきましょう。

import discord
import asyncio
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import os

# 楽々勤怠アカウント情報の取得
def get_credentials():
    file_path = "path/to/your/credentials.txt"
    with open(file_path, "r", encoding="utf-8") as file:
        lines = file.readlines()
    user_name = lines[0].strip() if len(lines) > 0 else None
    user_id = lines[1].strip() if len(lines) > 1 else None
    user_pass = lines[2].strip() if len(lines) > 2 else None
    return user_name, user_id, user_pass

# 楽々勤怠操作
def perform_action(discord_comment, retry_count=3):
    user_name, user_id, user_pass = get_credentials()
    send_message = "エラーが発生しました:情報なし"
    try:
        # WebDriverの設定
        url = "勤怠管理URL"
        options = Options()
        options.add_argument('--user-data-dir=path/to/your/profile')
        options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148')
        driver = webdriver.Chrome(options=options)
        driver.set_window_size(400, 1000)
        driver.get(url)

        # ログインプロセス
        sleep(5)
        driver.find_element(By.XPATH, "/html/body/div/div/form/dl/dd[1]/input").send_keys(user_name)
        driver.find_element(By.XPATH, "/html/body/div/div/form/dl/dd[2]/input").send_keys(user_id)
        driver.find_element(By.XPATH, "/html/body/div/div/form/dl/dd[3]/input").send_keys(user_pass)
        sleep(1)
        driver.find_element(By.XPATH, "/html/body/div/div/form/input[2]").click()
        print("login_complete")

        # 警告がある場合は閉じる
        try:
            sleep(7)
            driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[1]/span").click()
            print("alert_close")
        except:
            print("閉じるアラートがありません")

        # アクションの実行
        sleep(2)
        if discord_comment == "get_office":
            element = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/div[4]/span[1]")
            send_message = "get_office アクションが完了しました。"
        elif discord_comment == "leave_office":
            element = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[2]/div/div[4]/span[2]")
            send_message = "leave_office アクションが完了しました。"
        element.click()
        print("click button")

        # 位置情報送信確認プロンプトの処理
        sleep(1)
        try:
            WebDriverWait(driver, 10).until(EC.alert_is_present())
            alert = driver.switch_to.alert
            alert.accept()
        except:
            print("警告なし")

        sleep(7)
    except Exception as e:
        print("Error:", str(e))
        retry_count -= 1
        if retry_count > 0:
            print(f"Retrying... Attempts left: {retry_count}")
            return perform_action(discord_comment, retry_count)
        else:
            print("再試行回数が最大に達しました。終了しています。")
            send_message = "error:do not click button"
    finally:
        try:
            driver.quit()
        except:
            pass
    return send_message

# Discordボットの設定
TOKEN = 'YOUR_DISCORD_TOKEN'
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
intents.guilds = True
intents.members = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('ログインしました')
    print('参加しているサーバー(ギルド)の情報を取得します。')
    for guild in client.guilds:
        print(f'サーバー名: {guild.name}, ID: {guild.id}, メンバー数: {guild.member_count}')

@client.event
async def on_message(message):
    if message.content == 'get_office':
        print("get_office")
        send_message = perform_action("get_office")
        print("send_message", send_message)
        await message.channel.send(send_message)
    elif message.content == 'leave_office':
        print("leave_office")
        send_message = perform_action("leave_office")
        await message.channel.send(send_message)

async def main():
    async with client:
        await client.start(TOKEN)

if __name__ == "__main__":
    asyncio.run(main())

使用方法

  1. credentials.txtファイルを作成し、以下の形式で認証情報を記入します:

    ユーザー名
    ユーザーID
    パスワード
    
  2. Discordボットのトークンを取得し、YOUR_DISCORD_TOKENを実際のトークンに置き換えます。

  3. スクリプトを実行します。

  4. Discordサーバーで以下のコマンドを使用して操作を行います:

    • get_office: 出勤処理
    • leave_office: 退勤処理

注意事項

  • このスクリプトは教育目的で作成されています。実際の使用には、所属組織の規則や法令を遵守してください。
  • セキュリティ上の理由から、認証情報は安全に管理してください。
  • Seleniumの使用にはChromeDriverが必要です。適切にセットアップしてください。

まとめ

このスクリプトを使用することで、勤怠を自動化し、Discordを通じてリモートで簡単に出退勤の管理ができるようになります。Pythonの強力な機能を活用することで、日々の業務を効率化できる良い例となっています。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?