add examples

This commit is contained in:
relikd
2022-04-08 20:22:08 +02:00
parent 45dfc31966
commit e871e6f03e
10 changed files with 488 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env python3
from botlib.tgclient import TGClient
print('open a new telegram chat window with your bot and send /start')
TGClient.listen_chat_info(__API_KEY__, 'my-username')

View File

@@ -0,0 +1,61 @@
#!/usr/bin/env python3
from botlib.tgclient import TGClient
bot = TGClient(__API_KEY__, polling=True, allowedUsers=['my-username'])
@bot.message_handler(commands=['hi'])
def bot_reply(message):
if bot.allowed(message): # only reply to a single user (my-username)
bot.reply_to(message, 'Good evening my dear.')
@bot.message_handler(commands=['set'])
def update_config(message):
if bot.allowed(message):
try:
config = data_store.get(message.chat.id)
except KeyError:
bot.reply_to(message, 'Not found.')
return
if message.text == '/set day':
config.param = 'day'
elif message.text == '/set night':
config.param = 'night'
else:
bot.reply_to(message, 'Usage: /set [day|night]')
@bot.message_handler(commands=['start'])
def new_chat_info(message):
bot.log_chat_info(message.chat)
if bot.allowed(message):
if data_store.get(message.chat.id):
bot.reply_to(message, 'Already exists')
else:
CreateNew(message)
class CreateNew:
def __init__(self, message):
self.ask_name(message)
def ask_name(self, message):
msg = bot.send_force_reply(message.chat.id, 'Enter Name:')
bot.register_next_step_handler(msg, self.ask_interval)
def ask_interval(self, message):
self.name = message.text
msg = bot.send_buttons(message.chat.id, 'Update interval (minutes):',
options=[3, 5, 10, 15, 30, 60])
bot.register_next_step_handler(msg, self.finish)
def finish(self, message):
try:
interval = int(message.text)
except ValueError:
bot.send_abort_keyboard(message, 'Not a number. Aborting.')
return
print('Name:', self.name, 'interval:', interval)
bot.send_message(message.chat.id, 'done.')

View File

@@ -0,0 +1,54 @@
#!/usr/bin/env python3
from botlib.cron import Cron
from botlib.helper import Log
from botlib.oncedb import OnceDB
from botlib.tgclient import TGClient
# the pipeline process logic is split up:
# - you can have one file for generating the entries and writing to db (import)
# e.g., import an example from web-scraper and call download()
# - and another file to read db and send its entries to telegram (this file)
# of course, you can put your download logic inside this file as well
import sub_job_a as jobA
import sub_job_b as jobB
cron = Cron()
bot = TGClient(__API_KEY__, polling=True, allowedUsers=['my-username'])
bot.set_on_kill(cron.stop)
def main():
def clean_db(_):
Log.info('[clean up]')
OnceDB('cache.sqlite').cleanup(limit=150)
def notify_jobA(_):
jobA.download(topic='development', cohort='dev:py')
send2telegram(__A_CHAT_ID__)
def notify_jobB(_):
jobB.download()
send2telegram(__ANOTHER_CHAT_ID__)
# Log.info('Ready')
cron.add_job(10, notify_jobA) # every 10 min
cron.add_job(30, notify_jobB) # every 30 min
cron.add_job(1440, clean_db) # daily
cron.start()
# cron.fire()
def send2telegram(chat_id):
db = OnceDB('cache.sqlite')
# db.mark_all_done()
def _send(cohort, uid, obj):
Log.info('[push] {} {}'.format(cohort, uid))
return bot.send(chat_id, obj, parse_mode='HTML',
disable_web_page_preview=True)
if not db.foreach(_send):
# send() sleeps 45 sec (on error), safe to call immediatelly
send2telegram(chat_id)
main()