Compare commits
3 Commits
46fba104db
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3be2ba7688 | ||
|
|
8f240f0d56 | ||
|
|
08ada11e75 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
local_settings.py
|
||||
22
bot.py
Normal file
22
bot.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from telegram.ext import Application
|
||||
from telegram.ext import CommandHandler
|
||||
import syms as _bot_syms
|
||||
|
||||
async def bot_start(update, context):
|
||||
chat_id = update.effective_chat.id
|
||||
try: thread_id= update.message.message_thread_id
|
||||
except: thread_id= None
|
||||
txt= f'wb_alert bot: <code>{chat_id}</code>'
|
||||
if thread_id: txt= f'wb_alert bot: <code>{chat_id}</code> thread: <code>{thread_id}</code>'
|
||||
await update.message.reply_text(txt, parse_mode='HTML')
|
||||
print(f'alert_bot bot_start: {chat_id}')
|
||||
|
||||
def main():
|
||||
application = Application.builder().token(_bot_syms.TOKEN).build()
|
||||
application.add_handler(CommandHandler('start', bot_start))
|
||||
|
||||
print('alert_bot main: Bot is running...')
|
||||
application.run_polling()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
37
msg_sender.py
Normal file
37
msg_sender.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import requests as _rqsts
|
||||
import syms as _bot_syms
|
||||
|
||||
|
||||
def _wb_telegram_send_alert(message, chat_id, thread_id= None):
|
||||
print(f"wb_alert_bot _wb_telegram_send_alert: sending message to {chat_id}, {thread_id}...")
|
||||
payload = {
|
||||
'chat_id': chat_id,
|
||||
'text': message,
|
||||
'parse_mode': 'Markdown',
|
||||
}
|
||||
if thread_id: payload['message_thread_id']= thread_id
|
||||
|
||||
try:
|
||||
res = _rqsts.post(_bot_syms.TELEGRAM_API_URL, data=payload, timeout=3)
|
||||
res.raise_for_status()
|
||||
|
||||
jres = res.json()
|
||||
if jres.get('ok'):
|
||||
print(f"wb_alert_bot _wb_telegram_send_alert: message sent to {chat_id}, {thread_id}!")
|
||||
else:
|
||||
print(f"wb_alert_bot _wb_telegram_send_alert: Message not sent to {chat_id}, {thread_id}: {jres}")
|
||||
|
||||
except _rqsts.exceptions.RequestException as e:
|
||||
print(f"wb_alert_bot _wb_telegram_send_alert: Connection error {chat_id}, {thread_id}: {e}")
|
||||
|
||||
|
||||
def wb_telegram_send_alert(msg, chat_id, thread_id= None):
|
||||
_wb_telegram_send_alert(msg, chat_id, thread_id)
|
||||
|
||||
|
||||
def wb_telegram_send_alert_debug(msg):
|
||||
users= _bot_syms.USERS
|
||||
for user in users:
|
||||
chat_id= user[0]
|
||||
thread_id= user[1]
|
||||
_wb_telegram_send_alert(msg, chat_id, thread_id)
|
||||
2
requirements/requirements.txt
Normal file
2
requirements/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
python-telegram-bot[job-queue]==22.5
|
||||
requests==2.32.5
|
||||
13
syms.py
Normal file
13
syms.py
Normal file
@@ -0,0 +1,13 @@
|
||||
try:
|
||||
from local_settings import TOKEN
|
||||
except:
|
||||
print('alert_bot FATAL: add bot token here: local_settings.py')
|
||||
TOKEN= ''
|
||||
|
||||
try:
|
||||
from local_settings import USERS
|
||||
except:
|
||||
print('alert_bot FATAL: add bot users here: local_settings.py')
|
||||
USERS= []
|
||||
|
||||
TELEGRAM_API_URL = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
|
||||
Reference in New Issue
Block a user