Database features. Development has started.

This commit is contained in:
Alexandr Bogomyakov
2018-05-17 20:07:50 +03:00
parent 7341eaaa4e
commit 506978ea76
3 changed files with 147 additions and 0 deletions

94
bot.py Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Simple Bot to reply to Telegram messages.
This is built on the API wrapper, see echobot2.py to see the same example built
on the telegram.ext bot framework.
This program is dedicated to the public domain under the CC0 license.
"""
import os
import logging
import threading
import telegram
from telegram.error import NetworkError, Unauthorized
from time import sleep
try:
import tg_settings
except:
print("You have to create tg_settings.py. See tg_settings.py.example.")
os.exit(1)
try:
if os.environ["DRYRUN"]:
from dryrun import Perdoliq
except:
from main import Perdoliq
update_id = None
def main():
"""Run the bot."""
global update_id
# Telegram Bot Authorization Token
TOKEN = tg_settings.TG_TOKEN
bot = telegram.Bot(TOKEN)
# get the first pending update_id,
# this is so we can skip over it in case
# we get an "Unauthorized" exception.
try:
update_id = bot.get_updates()[0].update_id
except IndexError:
update_id = None
logging.basicConfig(level=logging.DEBUG)
while True:
try:
handle_update(bot)
except NetworkError:
sleep(1)
except Unauthorized:
# The user has removed or blocked the bot.
update_id += 1
def perdoliq(username, password, subj, test, acc, is_delayed):
try:
app = Perdoliq(username, password)
app.auth()
app.resolve(subj, test, acc, is_delayed=int(is_delayed))
except Exception as e:
return "Exception: " + str(e)
def list_test(username, password):
try:
app = Perdoliq(username, password)
app.auth()
return (app.get_tests())
except Exception as e:
return "Exception: " + str(e)
def handle_update(bot):
"""Echo the message the user sent."""
global update_id
# Request updates after the last update_id
for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
if update.message:
# if there is and update, process it in threads
t = threading.Thread(target=do_action, args=(update,))
t.start()
def do_action(update):
try:
s = update.message.text.split()
except:
return 1
# if s[0] == '/resolve':
# return 0
if __name__ == '__main__':
main()

41
database.py Normal file
View File

@ -0,0 +1,41 @@
import datetime as dt
class DataBase:
def __init__(self, basefile, scheme):
import sqlite3
self.scheme = ''
try:
self.conn = sqlite3.connect(basefile, check_same_thread=False)
except:
print('Could not connect to DataBase.')
return None
with open(scheme, 'r') as scheme_sql:
sql = scheme_sql.read()
self.scheme = sql
if self.conn is not None:
try:
cursor = self.conn.cursor()
cursor.executescript(sql)
except:
print('Could not create scheme.')
else:
print("Error! cannot create the database connection.")
print('DB created.')
def execute(self, sql):
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
return cursor.fetchall()
def update_user(self, user_name, user_id):
date = int(dt.datetime.now().strftime("%s"))
sql = """INSERT OR IGNORE INTO
users('user_id', 'user_name', 'date')
VALUES ('%s','%s','%s')""" % (
user_id,
user_name,
date
)
self.execute(sql)

12
scheme.sql Normal file
View File

@ -0,0 +1,12 @@
BEGIN TRANSACTION;
-- DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_id` TEXT NOT NULL,
`user_name` TEXT NOT NULL,
`site_name` TEXT,
`site_user` TEXT,
`site_pass` TEXT,
`date` INTEGER NOT NULL
);
COMMIT;