-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.py
More file actions
54 lines (44 loc) · 1.35 KB
/
cli.py
File metadata and controls
54 lines (44 loc) · 1.35 KB
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
import sqlite3
from pprint import pprint
from telebot import conf, telegram
from telebot.db import SQL
from telebot.models import Message, Update
options = conf.open_env()
# print(options) # debug
token = options["TELEGRAM_TOKEN"]
db_name = options.get("DBFILE", "telegram.db")
db = SQL(db_name)
def get_updates():
""" A wrapper que envuelve la funcionalidad
de obtener updates desde telegram
"""
updates = telegram.get_updates(token)
for _upt in updates:
update = Update(db)
try:
update.add(_upt["update_id"])
telegram.register_message(db, _upt["message"], token)
except sqlite3.IntegrityError:
print("Update ya registrado")
pprint(_upt) # debug
if __name__ == "__main__":
import sys
try:
action = sys.argv[1]
except IndexError:
print("Ingrese `init` `updates` o `send`")
sys.exit(1)
if action == "updates":
get_updates()
elif action == "send":
try:
chat_id = sys.argv[2]
text = sys.argv[3]
except IndexError:
print("ingrese `chat_id` y `text` para enviar")
sys.exit(1)
telegram.send_message(text, int(chat_id), token)
elif action == "init":
db.setup_db([Update.table, Message.table])
else:
print("Ingrese `init` `updates` o `send`")