-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
98 lines (65 loc) · 2.75 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import time
import math
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from pyrogram import Client, filters
from pyrogram.types import Message
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import random
from configparser import ConfigParser
parser = ConfigParser(os.getenv('APP_CONF_FILE'))
api_id = parser.get('API_ID')
api_hash = parser.get('API_HASH')
from types import Inspiration
api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
app = Client("session/bot", api_id = api_id, api_hash=api_hash)
subscriber_id = ''
subscriber_telegram_id = ''
start_time = time.time()
def randomInspiration():
pass
async def job():
global subscriber_telegram_id
global subscriber_id
if(app.is_initialized):
lifetime = math.ceil(time.time() - start_time)
if lifetime <= 3600:
message = f"My age: {math.floor(lifetime)} seconds"
elif 3600 <= lifetime and lifetime <= 86400:
message = f"My age: {math.floor(lifetime / 3600)} hours"
else:
message = f"My age: {math.floor(lifetime / 86400)} days"
await app.send_message(subscriber_id, randomInspiration() + f"\n{message}")
peer = subscriber_telegram_id if subscriber_telegram_id else subscriber_id
await app.send_message('djnotes', f'Inspiration sent to {peer}')
#TODO: Save message and sending time in database
# await app.send_message(id, f"My age: {time.time() - start_time} seconds")
scheduler = AsyncIOScheduler()
scheduler.add_job(job, "interval", seconds = 3)
@app.on_message(filters.private)
async def handle(client, message: Message):
global scheduler
global subscriber_id
global subscriber_telegram_id
if(message.text == '/start'):
subscriber_id = message.from_user.id
scheduler.start()
await client.send_message('djnotes', f'Inspiration sent to {subscriber_id}')
#TODO: Get user's Telegram ID and save it in receiver_telegram_id
#TODO: Save the user in database
if (message.text == '/send'):
# Simple use-case to test functionality by sending a random inspiration to the panel user
with Session(engine) as session:
inspirations = session.query(Inspiration).all()
selected = int (random.random() * len(inspirations))
message.reply(inspirations[selected].text)
elif(message.text == '/stop'):
scheduler.stop()
app.run()
# Load credentials
userEnv = open(os.getenv("MARIADB_USER")).read()
passwordEnv = open(os.getenv("MARIADB_PASSWORD")).read()
databaseEnv = open(os.getenv("MARIADB_DATABASE")).read()
engine = create_engine(f"mysql+pymysql://{userEnv}:{passwordEnv}@db/{databaseEnv}", echo=True, future=True)