init
This commit is contained in:
commit
86d65f801a
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
*.pyc
|
||||||
|
__pycache__
|
||||||
|
.DS_Store
|
||||||
|
.vercel
|
||||||
|
api/payload.json
|
6
README.md
Normal file
6
README.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
## Rainbow Welcome Bot
|
||||||
|
|
||||||
|
Используемые переменные среды:
|
||||||
|
|
||||||
|
BOT_TOKEN - токен бота созданный с помощью @BotFather
|
||||||
|
CHAT_ID - айди чата, который бот защищает (можно посмотреть в урле веб-версии клиента)
|
69
api/index.py
Normal file
69
api/index.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
from tgbot.rest import delete_message, register_webhook, send_message, ban_member, unban_member
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic.response import json, text
|
||||||
|
|
||||||
|
|
||||||
|
app = Sanic()
|
||||||
|
WEBHOOK = os.environ.get('VERCEL_URL')
|
||||||
|
CHAT_ID = os.environ.get('CHAT_ID').replace("-", "-100")
|
||||||
|
WELCOME_MSG = 'Добро пожаловть домой! Эта открытая беседа о Радужных Собраниях на русском языке. Пожалуйста, чтите Традиции и берегите друг друга.'
|
||||||
|
|
||||||
|
newcomers = {}
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=["GET", "PUT", "OPTIONS"])
|
||||||
|
async def register(req):
|
||||||
|
r = register_webhook(WEBHOOK)
|
||||||
|
print('\n\t\t\tWEBHOOK REGISTERED\n')
|
||||||
|
return json(r.json())
|
||||||
|
|
||||||
|
|
||||||
|
@app.post('/')
|
||||||
|
async def handle(req):
|
||||||
|
print(req)
|
||||||
|
try:
|
||||||
|
update = req.json
|
||||||
|
print(update)
|
||||||
|
msg = update.get('message')
|
||||||
|
if msg['chat']['id'] == CHAT_ID:
|
||||||
|
chat_id = msg['chat']['id']
|
||||||
|
member_id = msg['user']['id']
|
||||||
|
if msg['chat']['type'] == 'new_chat_member':
|
||||||
|
newcomers[member_id] = 'newcomer'
|
||||||
|
reply_markup = {
|
||||||
|
"inline_keyboard": [
|
||||||
|
[
|
||||||
|
{"text": "Всё понятно", "callback_data": "Всё понятно"},
|
||||||
|
{"text": "Доброе утро", "callback_data": "Доброе утро"}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
welcome_msg_id = send_message(
|
||||||
|
chat_id,
|
||||||
|
WELCOME_MSG,
|
||||||
|
reply_to=msg['message_id'],
|
||||||
|
reply_markup=reply_markup
|
||||||
|
)
|
||||||
|
newcomers[member_id] = 'newcomer' + welcome_msg_id
|
||||||
|
elif msg['chat']['type'] == 'text':
|
||||||
|
data = newcomers[member_id]
|
||||||
|
if data:
|
||||||
|
if data.startswith('newcomer'):
|
||||||
|
if 'text' in msg:
|
||||||
|
answer = msg['text']
|
||||||
|
if 'доброе утро' in answer.lower():
|
||||||
|
del newcomers[member_id]
|
||||||
|
else:
|
||||||
|
delete_message(msg['message_id'])
|
||||||
|
else:
|
||||||
|
callback_data = update['callback_query']['data']
|
||||||
|
if callback_data == 'Всё понятно':
|
||||||
|
ban_member(member_id, chat_id)
|
||||||
|
elif callback_data == 'Доброе утро':
|
||||||
|
del newcomers[member_id]
|
||||||
|
delete_message(CHAT_ID, data.replace('newcomer', ''))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return text('ok')
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
sanic==19.6.0
|
||||||
|
requests
|
52
tgbot/rest.py
Normal file
52
tgbot/rest.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
TOKEN = os.environ.get('BOT_TOKEN')
|
||||||
|
apiBase = f"https://api.telegram.org/bot{TOKEN}/"
|
||||||
|
|
||||||
|
|
||||||
|
def register_webhook(url):
|
||||||
|
return requests.get(
|
||||||
|
apiBase + f'setWebhook?url={url}'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_message(cid, mid):
|
||||||
|
return requests.post(
|
||||||
|
apiBase + f"deleteMessage?chat_id={cid}&message_id={mid}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def send_message(cid, body, reply_to=None, reply_markup=None):
|
||||||
|
url = apiBase + f"sendMessage?chat_id={cid}&text={body}"
|
||||||
|
if reply_to:
|
||||||
|
url += f'&reply_to_message_id={reply_to}'
|
||||||
|
if reply_markup:
|
||||||
|
reply_markup = json.dumps(reply_markup)
|
||||||
|
reply_markup = requests.utils.quote(reply_markup)
|
||||||
|
url += f'&reply_markup={reply_markup}'
|
||||||
|
r = requests.post(url)
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
# https://core.telegram.org/bots/api#banchatmember
|
||||||
|
def ban_member(chat_id, user_id, until_date=None):
|
||||||
|
url = apiBase + f"banChatMember?chat_id={chat_id}&user_id={user_id}"
|
||||||
|
if until_date:
|
||||||
|
url += f'&until_data={until_date}'
|
||||||
|
r = requests.post(url)
|
||||||
|
return r
|
||||||
|
|
||||||
|
# https://core.telegram.org/bots/api#unbanchatmember
|
||||||
|
def unban_member(chat_id, user_id):
|
||||||
|
url = apiBase + f"unbanChatMember?chat_id={chat_id}&user_id={user_id}&only_if_banned=1"
|
||||||
|
r = requests.post(url)
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def forward_message(cid, mid, to_chat_id):
|
||||||
|
url = apiBase + f"forwardMessage?chat_id={to_chat_id}" + \
|
||||||
|
f"&from_chat_id={cid}&message_id={mid}"
|
||||||
|
r = requests.post(url)
|
||||||
|
return r
|
15
vercel.json
Executable file
15
vercel.json
Executable file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"functions": {
|
||||||
|
"api/index.py": {
|
||||||
|
"memory": 1024,
|
||||||
|
"maxDuration": 10
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"src": "/",
|
||||||
|
"dest": "/api/index.py"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user