Skip to content

Commit cb703b0

Browse files
committed
initial version
0 parents  commit cb703b0

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.8-alpine
2+
3+
# install pipenv, gcc, devel
4+
5+
RUN apk add --no-cache --virtual .build-deps \
6+
gcc \
7+
musl-dev \
8+
libffi-dev \
9+
openssl-dev \
10+
python3-dev \
11+
cargo \
12+
&& pip install pipenv \
13+
&& apk del .build-deps
14+
15+
16+
WORKDIR /code
17+
18+
ADD Pipfile* /code/
19+
RUN pipenv sync
20+
CMD ["pipenv", "run", "python", "handler.py"]

Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
python-telegram-bot = "==13.12"
8+
9+
[dev-packages]
10+
11+
[requires]
12+
python_version = "3.8"

Pipfile.lock

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Readme.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ToBeDo: Telegram simple checklist bot
2+
3+
Want to use deployed version?
4+
5+
Create a new channel in telegram for your checklists and invite https://t.me/tobedo_bot there.
6+
The bot will automatically turn any messages into a checklist
7+
8+
# Deployment of own bot
9+
10+
1) Go to https://telegram.me/BotFather and add a new bot. Remember bot username, and API token
11+
2) Deploy tobedo docker file to some server and pass environment variable TG_TOKEN:
12+
13+
```sh
14+
docker run tobedo -e TG_TOKEN=<your token>
15+
```
16+
17+
Compose example:
18+
19+
```yaml
20+
version: '3.3'
21+
22+
services:
23+
tobedo:
24+
image: tobedo
25+
environemnt:
26+
- TG_TOKEN=<your token>
27+
```

handler.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
3+
import logging
4+
5+
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
6+
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
7+
import json
8+
import hashlib
9+
import os
10+
11+
logger = logging.getLogger(__name__)
12+
13+
TOKEN = os.environ.get('TG_TOKEN')
14+
if not TOKEN:
15+
print('TG_TOKEN not specified in env, please set TG_TOKEN with your bot token generated by @BotFather')
16+
exit(1)
17+
18+
def button_click(update, context):
19+
query = update.callback_query
20+
21+
if query.data.startswith('toggle__'):
22+
hash = query.data.replace('toggle__', '')
23+
24+
for i, btn in enumerate(query.message.reply_markup.inline_keyboard):
25+
checked = btn[0].text.startswith('☑️')
26+
btn_text = btn[0].text.replace('🟨 ', '').replace('☑️ ', '')
27+
if md5hash(
28+
f'{btn_text}_{i}'
29+
) == hash:
30+
print('found a btn', btn_text)
31+
new_text = f'☑️ {btn_text}' if not checked else f'🟨 {btn_text}'
32+
btn[0].text = new_text
33+
break
34+
35+
context.bot.edit_message_text(
36+
chat_id=query.message.chat_id,
37+
message_id=query.message.message_id,
38+
text=f'Click to toggle',
39+
reply_markup=query.message.reply_markup
40+
)
41+
42+
43+
44+
def md5hash(text):
45+
return hashlib.md5(text.encode('utf-8')).hexdigest()
46+
47+
48+
49+
def echo(update: Update, context: CallbackContext) -> None:
50+
"""
51+
This function would be added to the dispatcher as a handler for messages coming from the Bot API
52+
"""
53+
54+
msg = update.channel_post.text
55+
print('msg', msg)
56+
# Print to console
57+
58+
if update.channel_post and update.channel_post.text:
59+
lines = msg.split('\n')
60+
keyboard = []
61+
62+
index = 0
63+
for line in lines:
64+
if line.strip() == '':
65+
continue
66+
keyboard.append([InlineKeyboardButton(
67+
f"🟨 {line}",
68+
callback_data=f"toggle__{md5hash(f'{line}_{index}')}"
69+
)])
70+
index += 1
71+
72+
73+
reply_markup = InlineKeyboardMarkup(keyboard)
74+
update.channel_post.reply_text('Click to toggle', reply_markup=reply_markup)
75+
76+
else:
77+
pass
78+
# This is equivalent to forwarding, without the sender's name
79+
# update.message.copy(update.message.chat_id)
80+
81+
82+
83+
def main() -> None:
84+
updater = Updater(TOKEN)
85+
86+
# Get the dispatcher to register handlers
87+
# Then, we register each handler and the conditions the update must meet to trigger it
88+
dispatcher = updater.dispatcher
89+
90+
dispatcher.add_handler(CallbackQueryHandler(button_click))
91+
92+
93+
# Echo any message that is not a command
94+
dispatcher.add_handler(MessageHandler(~Filters.command, echo))
95+
96+
# Start the Bot
97+
updater.start_polling()
98+
99+
# Run the bot until you press Ctrl-C
100+
updater.idle()
101+
102+
103+
if __name__ == '__main__':
104+
main()

publish_to_dockerhub.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
docker buildx create --use
3+
docker buildx build --platform=linux/amd64,linux/arm64 --tag "devforth/tobedo:latest" --tag "devforth/tobedo:1.0.1" --push .

0 commit comments

Comments
 (0)