-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
37 lines (28 loc) · 973 Bytes
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
from os import environ
from telegram import Bot
from telegram.error import BadRequest
from models import post
import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logging.getLogger('requests').setLevel(logging.WARNING)
logger = logging.getLogger('@hardmob_promo')
def main():
bot = Bot(environ.get('TOKEN'))
chat_id = environ.get('CHAT_ID')
post.updatedb()
new_posts = post.get_posts(sent_only=False)
if new_posts:
for p in new_posts:
try:
bot.sendMessage(chat_id=chat_id, text=p.text(), parse_mode='HTML')
post.mark_as_sent(p.uid)
except BadRequest:
logging.info('Bad post formatting: %d' % p.uid)
logger.info('%d new post(s) have been sent!' % len(new_posts))
else:
logger.info('No new posts at this time!')
if __name__ == '__main__':
main()