Skip to content

Commit 17e2ae6

Browse files
committed
initial commit
Signed-off-by: call-me-matt <[email protected]>
0 parents  commit 17e2ae6

8 files changed

+393
-0
lines changed

Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM python
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY requirements.txt ./
6+
RUN pip3 install --no-cache-dir -r requirements.txt
7+
8+
#COPY ./src .
9+
10+
CMD [ "python", "./osmChangesetsBot.py" ]

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# osmChangeMonitorBot
2+
3+
this is a bot for telegram that monitors changes for a list of openstreetmap users.
4+
5+
you can customize your list and requests stats by sending `/stats`. Additionally, if a user exceeds 300 or 1000 changes in the current month, a notification message is sent.
6+
7+
## installation
8+
9+
* create a bot token for telegram messenger: https://t.me/botfather
10+
11+
* add the token to the secrets.env-File
12+
13+
* use docker-compose to start: `docker-compose up`
14+
15+
## demo
16+
17+
or try it out (only working if my instance is running): https://t.me/osmChangesetBot
18+

docker-compose.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: '3'
2+
services:
3+
bot:
4+
build: .
5+
restart: unless-stopped
6+
env_file:
7+
- secrets.env
8+
volumes:
9+
- ./src:/usr/src/app

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-telegram-bot
2+
requests

secrets.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TELEGRAM_BOT_TOKEN=yourtoken

src/databaseHandler.py

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import sqlite3
5+
import logging
6+
import datetime
7+
8+
logging.basicConfig(format='[%(levelname)s] %(name)s: %(message)s',level=logging.INFO)
9+
logger = logging.getLogger("database-handler")
10+
11+
def init():
12+
logger.info("initializing database")
13+
con = sqlite3.connect('registration.db')
14+
db = con.cursor()
15+
db.execute("CREATE TABLE IF NOT EXISTS users( \
16+
user TINYTEXT PRIMARY KEY, \
17+
chatid TINYTEXT \
18+
)")
19+
con.commit()
20+
db.execute("CREATE TABLE IF NOT EXISTS osmStats( \
21+
user TINYTEXT PRIMARY KEY, \
22+
changes TINYTEXT \
23+
)")
24+
con.commit()
25+
db.execute("CREATE TABLE IF NOT EXISTS watchers( \
26+
telegramUser TINYTEXT, \
27+
osmUser TINYTEXT \
28+
)")
29+
con.commit()
30+
con.close()
31+
32+
def addUser(username, chatid):
33+
logger.info("adding telegram user " + username + " to database")
34+
con = sqlite3.connect('registration.db')
35+
db = con.cursor()
36+
db.execute("INSERT OR IGNORE INTO users (user,chatid) VALUES (?,?)",([username,chatid]))
37+
con.commit()
38+
con.close()
39+
40+
def removeUser(username):
41+
logger.info("removing telegram user " + username + " from database")
42+
con = sqlite3.connect('registration.db')
43+
db = con.cursor()
44+
db.execute("DELETE FROM users WHERE user=?",([username]))
45+
db.execute("DELETE FROM watchers WHERE telegramUser=?",([username]))
46+
db.execute("DELETE FROM osmStats WHERE user not in (SELECT DISTINCT osmUser from watchers)")
47+
con.commit()
48+
con.close()
49+
50+
def isUserRegistered(telegramUser):
51+
con = sqlite3.connect('registration.db')
52+
db = con.cursor()
53+
db.execute("SELECT chatid FROM users WHERE user=?",([telegramUser]))
54+
entries = db.fetchall()
55+
con.close()
56+
if entries == None or entries == []:
57+
return False
58+
return True
59+
60+
def addWatcher(telegramUser, osmUser):
61+
logger.info("adding follower " + telegramUser + " for " + osmUser + " to the database")
62+
con = sqlite3.connect('registration.db')
63+
db = con.cursor()
64+
#TODO: only if not existing
65+
db.execute("INSERT INTO watchers (telegramUser,osmUser) VALUES (?,?)",([telegramUser,osmUser]))
66+
con.commit()
67+
con.close()
68+
69+
def getWatcher(osmUser):
70+
con = sqlite3.connect('registration.db')
71+
db = con.cursor()
72+
db.execute("SELECT chatid FROM users WHERE user in (SELECT DISTINCT telegramUser FROM watchers WHERE osmUser=?)",([osmUser]))
73+
entries = db.fetchall()
74+
con.close()
75+
result = []
76+
for entry in entries:
77+
result.append(entry[0])
78+
return result
79+
80+
def removeWatcher(telegramUser, osmUser):
81+
logger.info("removing follower " + telegramUser + " for " + osmUser + " from the database")
82+
con = sqlite3.connect('registration.db')
83+
db = con.cursor()
84+
db.execute("DELETE FROM watchers WHERE telegramUser=? AND osmUser=?",([telegramUser,osmUser]))
85+
db.execute("DELETE FROM osmStats WHERE user not in (SELECT DISTINCT osmUser from watchers)")
86+
con.commit()
87+
con.close()
88+
89+
def getOsmUsers(telegramUser=None):
90+
con = sqlite3.connect('registration.db')
91+
con.row_factory = sqlite3.Row
92+
db = con.cursor()
93+
if telegramUser:
94+
db.execute("SELECT DISTINCT osmUser from watchers WHERE telegramUser=?",([telegramUser]))
95+
else:
96+
db.execute("SELECT DISTINCT osmUser from watchers")
97+
entries = db.fetchall()
98+
con.close()
99+
result = []
100+
for entry in entries:
101+
result.append(entry['osmUser'])
102+
return result
103+
104+
def getStats(watcher):
105+
logger.debug("getStats for " + str(watcher))
106+
con = sqlite3.connect('registration.db')
107+
con.row_factory = sqlite3.Row
108+
db = con.cursor()
109+
db.execute("SELECT user,changes FROM osmStats WHERE user in (SELECT osmUser from watchers WHERE telegramUser=?)",([watcher]))
110+
entries = db.fetchall()
111+
con.close()
112+
result = ""
113+
for (user,changes) in entries:
114+
result += user + ": " + str(changes) + " \n"
115+
return result
116+
117+
def updateStats(user, counter):
118+
logger.debug('updating stats for ' + user + ': ' + str(counter))
119+
con = sqlite3.connect('registration.db')
120+
con.row_factory = sqlite3.Row
121+
db = con.cursor()
122+
db.execute("SELECT changes FROM osmStats WHERE user=?",([user]))
123+
counterOld = db.fetchone()
124+
db.execute("INSERT INTO osmStats (user, changes) VALUES(?,?) ON CONFLICT(user) DO UPDATE SET changes=?",([user,counter,counter]))
125+
con.commit()
126+
con.close()
127+
if counterOld == None:
128+
return -1
129+
else:
130+
return counterOld[0]
131+
132+

src/osmChangesetsBot.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import logging
5+
import sys
6+
import time
7+
import threading
8+
9+
import telegramHandler
10+
11+
logging.basicConfig(format='[%(levelname)s] %(name)s: %(message)s',level=logging.INFO)
12+
logger = logging.getLogger("main")
13+
14+
logger.info('starting osmChangesetsBot')
15+
16+
logger.debug('creating telegram-handler thread')
17+
telegramThread = telegramHandler.telegramHandler()
18+
telegramThread.daemon = True
19+
telegramThread.start()
20+
21+
while True:
22+
try:
23+
time.sleep(5)
24+
except KeyboardInterrupt:
25+
logger.info('Exiting...')
26+
sys.exit()
27+
except:
28+
raise
29+

0 commit comments

Comments
 (0)