ホーム 暮らし How I made my personal logging system smarter than expected by connecting it to LINE

How I made my personal logging system smarter than expected by connecting it to LINE

真鍋 千夏 真鍋 千夏 • 更新: • 公開: • 読了約 12 分
目次

In my previous article, I built a system that constantly logs my facial expressions and audio using a Raspberry Pi and Claude. This is a record of the additional development I did after using it for two days and realizing, 'I want it to do this too.'

What I added this time

n69bd0cfaa4ed
1. Made it possible to ask 'How was today?' via LINE

I created a /webhook endpoint in Flask and connected it to the LINE Messaging API. When I send a message from LINE, it passes the logs from the current day and the previous day to Claude, which returns an answer within 300 characters.

log_text = '\n\n'.join(filter(None, [get_log_text(today), get_log_text(yesterday)]))
prompt = f"LINE_BOT_PROMPT\n\n質問:user_text"

At first, I was only passing the current day's logs, which led to the problem of it not answering when I asked about yesterday. The cause was that I was connecting them with or, so if there were logs for today, the previous day's logs were skipped. I fixed it by joining both and passing them together.

Stumbling block: 'Use Webhooks' was OFF on the LINE side

Even though I created a channel in the LINE Developers Console and set the Webhook URL, it didn't work. The cause was that 'Use Webhooks' in the Official Account Manager was OFF by default. The fact that the developer console and the management console are separate is a point of confusion.

Also, a mysterious message saying 'I'm sorry, but this account does not accept individual inquiries' was returned first. This is LINE's default automatic response. It disappeared once I switched the response mode to 'Bot'.

2. Send interesting news to LINE when found

I connected it to a news watcher (miniops) that I was running as a separate project. When an article is found that Claude rates with a score of 4 or higher, a push notification is sent to LINE.

def _notify_interesting_news_via_line(before_ts: float):
 rows = conn.execute("""
 SELECT title, url, reason, score FROM watched_items
 WHERE interested = 1 AND score >= 4
 AND strftime('%s', created_at) > ?
 ORDER BY id DESC LIMIT 3
 """, (str(int(before_ts)),)).fetchall()
 for row in rows:
 title = _translation_cache.get(row['title']) or row['title']
 msg = f" 気になるニュース(興味度row['score']/5)\ntitle\n\nrow['reason']\n\nrow['url']"
 _line_push(msg)

The design records time.time() before the watcher runs, and after completion, it only picks up new arrivals since that timestamp.

3. Recommend a walk when fatigue builds up

I implemented the 'push notification when fatigue continues' feature that I wrote about in the 'things I want to do in the future' section of my previous article.

A thread running every 5 minutes checks two conditions:

  • When a tired face continues for 45 consecutive minutes

  • When there are 3 or more negative voice memos in the last hour (e.g., 'exhausted,' 'tired,' 'this is rough')

When either becomes true, a message recommending a walk is sent to LINE. There is a cooldown of once per hour.

WALK_MESSAGES = [
 " ちょっと散歩してみない?外の空気を吸うと気分が変わるよ!",
 " 疲れが溜まってるみたい。10分だけ外に出てみて!",
 ...
]

For the facial expression log, I just needed to reference the existing tired_streak_start global variable. For the negative tags in the audio log, I am querying the DB directly to aggregate them.

Handling LINE User IDs

To send push notifications, a user ID is required. While there is a way to write it in a configuration file, I used a method of automatically saving it when a webhook event is received.

user_id = event.get('source', ).get('userId', '')
if user_id:
 _save_line_user_id(user_id)

The moment you first message from LINE, the ID is saved to .line_user_id. Subsequent pushes are sent to that ID. This reduces the setup process by one step.

Impressions after using it

When I send "How was yesterday?" to LINE first thing in the morning, it replies based on the previous day's emotional fluctuations and voice memos. It's interesting to see patterns of fatigue I hadn't noticed myself, like "You seemed to be getting tired around 2:00 PM."

I'm looking forward to seeing if the walk suggestion triggers for the first time today.

Remaining tasks

  • Tuning the threshold for walk suggestions (45 minutes might be too long)

  • Want to be able to receive weekly reports via LINE

  • Want to more intelligently exclude logs from after 8:00 PM when family members' voices get mixed in

同じカテゴリの記事

真鍋 千夏
デジタルマーケター 真鍋 千夏
プロフィールを見る →

美容と暮らしの話題を担当。メーカーの公式発表、商品成分の公表資料、専門家の監修記事をもとに事実を整理します。

おすすめの関連記事