35 lines
1.0 KiB
Makefile

SHELL=/bin/bash
ALL_UPDATE_IDS_SRC=$(shell ls data/*.update_id.json)
ALL_UPDATE_IDS=$(subst .update_id,.processed_id,$(ALL_UPDATE_IDS_SRC))
all: fetch_updates process_updates
echo "Done"
process_updates: $(ALL_UPDATE_IDS)
%.processed_id.json: %.update_id.json process_message.php
@echo "<?php require __DIR__.'/lib/main.php'; require 'process_message.php'; extract(json_decode(file_get_contents('$<'),1)[\"message\"]); process_message(\$$from, \$$chat, \$$date, \$$text);" | php > $@
fetch_updates:
curl -s "https://api.telegram.org/bot$(TELEGRAM_BOT_TOKEN)/getUpdates" \
| jq -c '.result[]' \
| while read -r update_msg; \
do \
file="data/$$(echo $$update_msg | jq -c '.update_id').update_id.json"; \
if [ -f "$$file" ]; then \
old_md5=$$(md5sum "$$file" | cut -d' ' -f1); \
new_md5=$$(echo "$$update_msg" | md5sum | cut -d' ' -f1); \
if [ "$$old_md5" != "$$new_md5" ]; then \
echo "$$update_msg" > "$$file"; \
fi; \
else \
echo "$$update_msg" > "$$file"; \
fi; \
done
clean:
rm data/*.json