Skip to content

Commit 8b5a7f9

Browse files
authored
Multiple Image and Chat Support
Thanks @1Danish-00
1 parent d854e29 commit 8b5a7f9

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

Configs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from decouple import config
22

3+
34
class Var:
45
API_ID = config("API_ID", default=6)
56
API_HASH = config("API_HASH", None)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#### A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat.
77

8-
* For Getting ENV's Refer this [Link](https://New-dev0.Github.io/TgTwitterBot) !
8+
* For Getting ENV's Refer this [Link](https://new-dev0.github.io/tgtwitterbot) !
99
* All Enviroment Variables can be found in [`env.sample`](https://github.com/New-dev0/TgTwitterStreamer/blob/main/env.sample0) !
1010

1111
## Deploy to Heroku

main.py

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
# GNU General Public License v3.0
44

55

6+
import logging
7+
import re
8+
69
import tweepy
7-
from Configs import Var
810
from telethon import TelegramClient, events
911
from telethon.tl.custom import Button
10-
1112
from tweepy.asynchronous import AsyncStream
1213

13-
import logging
14+
from Configs import Var
15+
1416
logging.basicConfig(level=logging.INFO)
1517

1618

@@ -37,23 +39,78 @@ class TgStreamer(AsyncStream):
3739
async def on_connect(self):
3840
print("<<<---||| Stream Connected |||--->>>")
3941

42+
def get_urls(self, media):
43+
if not media:
44+
return []
45+
return [m["media_url_https"] for m in media if m["type"] == "photo"]
46+
4047
async def on_status(self, status):
4148
tweet = status._json
49+
if tweet["text"].startswith("RT "):
50+
return
4251
user = tweet["user"]
4352
if not str(user["id"]) in TRACK_IDS:
4453
return
45-
if tweet["text"].startswith("RT "):
46-
return
54+
pic, content = [], ""
55+
try:
56+
entities = tweet.get("entities", {}).get("media")
57+
extended_entities = tweet.get("extended_entities", {}).get("media")
58+
extended_tweet = (
59+
tweet.get("extended_tweet", {}).get("entities", {}).get("media")
60+
)
61+
all_urls = set()
62+
for media in (entities, extended_entities, extended_tweet):
63+
urls = self.get_urls(media)
64+
all_urls.update(set(urls))
65+
for pik in all_urls:
66+
pic.append(pik)
67+
content = tweet.get("extended_tweet").get("full_text")
68+
except BaseException:
69+
pass
4770
text = f"[{user['name']}](https://twitter.com/{user['screen_name']})"
4871
mn = " Tweeted :"
49-
text += mn + "\n\n" + f"`{tweet['text']}`"
72+
if content and (len(content) < 1000):
73+
text += mn + "\n\n" + f"`{content}`"
74+
else:
75+
text += mn + "\n\n" + f"`{tweet['text']}`"
5076
url = f"https://twitter.com/{user['screen_name']}/status/{tweet['id']}"
51-
await Client.send_message(
52-
int(Var.TO_CHAT),
53-
text,
54-
link_preview=False,
55-
buttons=Button.url(text="View 🔗", url=url),
56-
)
77+
multichat = Var.TO_CHAT.split()
78+
for chat in multichat:
79+
try:
80+
chat = int(chat)
81+
except BaseException:
82+
pass
83+
try:
84+
if pic:
85+
if len(pic) == 1:
86+
for pic in pic:
87+
await Client.send_message(
88+
chat,
89+
text,
90+
link_preview=False,
91+
file=pic,
92+
buttons=Button.url(text="View 🔗", url=url),
93+
)
94+
else:
95+
await Client.send_file(
96+
chat,
97+
file=pic,
98+
)
99+
await Client.send_message(
100+
chat,
101+
text,
102+
link_preview=False,
103+
buttons=Button.url(text="View 🔗", url=url),
104+
)
105+
else:
106+
await Client.send_message(
107+
chat,
108+
text,
109+
link_preview=False,
110+
buttons=Button.url(text="View 🔗", url=url),
111+
)
112+
except Exception as er:
113+
print(er)
57114

58115
async def on_connection_error(self):
59116
print("<<---|| Connection Error ||--->>")
@@ -65,25 +122,28 @@ async def on_exception(self, exception):
65122
@Client.on(events.NewMessage(pattern=r"/start"))
66123
async def startmsg(event):
67124
await event.reply(
68-
"Hi, I am Alive !",
125+
file="ult.webp",
69126
buttons=[
127+
[Button.inline("Hello Sir i'm Alive", data="ok")],
70128
[
71129
Button.url(
72-
"TgTwitterStreamer",
130+
"Source",
73131
url="https://github.com/New-dev0/TgTwitterStreamer",
74-
)
132+
),
133+
Button.url("Support Group", url="t.me/FutureCodesChat"),
75134
],
76-
[Button.url("Support Group", url="t.me/FutureCodesChat")],
77135
],
78136
)
79137

80138

139+
@Client.on(events.callbackquery.CallbackQuery(data=re.compile("ok")))
140+
async def _(e):
141+
return await e.answer("I'm Alive , No Need to click button 😂😂")
142+
143+
81144
if __name__ == "__main__":
82145
Stream = TgStreamer(
83-
Var.CONSUMER_KEY,
84-
Var.CONSUMER_SECRET,
85-
Var.ACCESS_TOKEN,
86-
Var.ACCESS_TOKEN_SECRET
146+
Var.CONSUMER_KEY, Var.CONSUMER_SECRET, Var.ACCESS_TOKEN, Var.ACCESS_TOKEN_SECRET
87147
)
88148
Stream.filter(follow=TRACK_IDS)
89149

ult.webp

22.7 KB
Loading

0 commit comments

Comments
 (0)