botonotes

Get your notes from telegram right into your local files.
git clone git://git.konyahin.xyz/botonotes
Log | Files | Refs | LICENSE

commit bbdeb71c1cbcc72b35a9bc0b5a06892e68616005
Author: Anton Konyahin <me@konyahin.xyz>
Date:   Fri,  8 Mar 2024 10:30:18 +0300

initial commit

Diffstat:
ALICENSE | 21+++++++++++++++++++++
Abotonotes | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2024 Anton Konyahin me@konyahin.xyz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/botonotes b/botonotes @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import datetime +import json +import os +import requests +import subprocess + +master = int(subprocess.check_output(["pass", "botonotes/user"])) +token = subprocess.check_output(["pass", "botonotes/token"]).decode("utf-8").strip("\n") + +def url(method): + return "https://api.telegram.org/bot{}/{}".format(token, method) + +def getUpdates(maxid): + params = {} + if maxid: + params["offset"] = maxid + 1 + + answer = json.loads(requests.get(url("getUpdates"), params = params).content) + if (not answer["ok"]): + print("Got error: ", answer["description"]) + exit() + + return answer["result"] + +def sendMessage(chat, message): + requests.get(url("sendMessage"), params = {"chat_id": chat, "text": message}) + +def saveNote(note): + text = note["message"]["text"] + date = note["message"]["date"] + file = datetime.datetime.fromtimestamp(date).strftime("%Y-%m-%d-%H%M%S.md") + path = os.environ["NOTES_QUEUE"] + "/" + file + with open(path, 'a+') as f: + f.write(text + "\n\n") + +maxid = 0 +notes = 0 +strangers = set() + +while True: + updates = getUpdates(maxid) + if (not updates): + break + + for update in updates: + id = update["update_id"] + if id > maxid: + maxid = id + + if "message" not in update: + continue + + user = update["message"]["from"]["id"] + chat = update["message"]["chat"]["id"] + + if user != master: + strangers.add(chat) + continue + + saveNote(update) + notes += 1 + +if notes: + sendMessage(master, "Got!") + +print("Got {} notes.".format(notes)) + +for stranger in strangers: + sendMessage(stranger, "Sorry, this is a private bot.") + +print("Greeted {} strangers.".format(len(strangers)))