-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtele_bot.py
60 lines (48 loc) · 1.69 KB
/
tele_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# import urllib.parse
import time
try:
from secret import *
except:
import os
import telegram
import asyncio
bot_api = os.environ['bot_api']
chat_id = os.environ['chat_id']
bot = telegram.Bot(token=bot_api)
async def sendMsg(text, extra):
for i in range(0, len(text), 2000):
time.sleep(5)
# txt = urllib.parse.quote(txt)
try:
await bot.send_message(chat_id, text[i:i + 2000], parse_mode='html', disable_web_page_preview=True)
except Exception as e:
print(str(e))
if len(extra)>0:
try:
await bot.send_message(chat_id, extra, parse_mode='html')
except Exception as e:
print(str(e))
async def sendPhoto(imgs, text):
# Convert the first image to an InputMediaPhoto with a caption
imgs[0] = telegram.InputMediaPhoto(str(imgs[0]), caption=text, parse_mode='HTML')
# Create a list to store all the media objects
media_group = [imgs[0]]
# Convert the rest of the images to InputMediaPhoto objects
for i in range(1, len(imgs)):
media_group.append(telegram.InputMediaPhoto(str(imgs[i])))
# Send the media group
try:
await bot.send_media_group(chat_id, media_group)
except:
print('photo didnt go...',imgs[0])
async def main(post):
sender = '<a href="https://fb.com/groups/'+os.environ['group_link']+'/permalink/'+post['link']+'">' + post['sender'] + '</a>'
txt = post['text']
# txt += post['extra']
if len(txt)>0:
await sendMsg(sender + txt, post['extra'])
if len(post['image'])>0:
await sendPhoto(post['image'], sender)
def sendPost(post):
loop = asyncio.get_event_loop()
loop.run_until_complete(main(post))