103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
import sys, random
|
|
import traceback
|
|
from typing import Sequence, AnyStr
|
|
from depytg import types, methods, errors
|
|
|
|
HASHTAGS = {
|
|
"#TeamElio": (
|
|
'https://s9.postimg.org/p89n5hzz3/photo_2018-01-29_21-47-01.jpg',
|
|
'https://s9.postimg.org/hsadjpjzj/photo_2018-01-29_21-47-11.jpg',
|
|
'https://s9.postimg.org/50w7d7xcv/photo_2018-01-29_21-47-17.jpg',
|
|
'https://s9.postimg.org/iht5w2s8v/photo_2018-01-29_21-47-24.jpg',
|
|
'https://s9.postimg.org/dvx1nqtv3/photo_2018-01-29_21-47-39.jpg',
|
|
),
|
|
"#TeamOliver": (
|
|
'http://filmmakermagazine.com/wp-content/uploads/2017/10/t-call-me-by-your-name.jpg',
|
|
'https://s9.postimg.org/r02m0f6hb/photo_2018-01-29_21-47-32.jpg',
|
|
),
|
|
"#CuloDiElio": (
|
|
'https://s9.postimg.org/dvx1nqtv3/photo_2018-01-29_21-47-39.jpg',
|
|
'https://s9.postimg.org/iht5w2s8v/photo_2018-01-29_21-47-24.jpg',
|
|
)
|
|
}
|
|
|
|
HELP_TEXT = """A bot that sends a picture of Elio whenever somebody types #TeamElio
|
|
Made with ❤ by @Depaulicious 🏳️🌈
|
|
|
|
Source code: https://git.depau.eu/Depaulicious/CallMeByYourBot
|
|
Based on DepyTG: https://github.com/Depaulicious/DepyTG"""
|
|
|
|
|
|
def send_help(token: AnyStr, msg: types.Message):
|
|
send_msg = methods.sendMessage(msg.chat.id, HELP_TEXT)
|
|
send_msg.reply_to_message_id = msg.message_id
|
|
send_msg["disable_web_page_preview"] = True
|
|
send_msg(token)
|
|
|
|
|
|
def parse_message(token: AnyStr, msg: types.Message):
|
|
if not 'text' in msg:
|
|
return
|
|
|
|
if "/help" in msg.text:
|
|
if "group" in msg.chat.type:
|
|
username = methods.getMe()(token).username
|
|
|
|
if msg.text == "/help@{}".format(username):
|
|
send_help(token, msg)
|
|
|
|
elif msg.text == "/help":
|
|
send_help(token, msg)
|
|
|
|
for hashtag in HASHTAGS:
|
|
if hashtag.lower() in msg.text.lower():
|
|
send_photo = methods.sendPhoto(chat_id=msg.chat.id)
|
|
send_photo.reply_to_message_id = msg.message_id
|
|
send_photo.photo = random.choice(HASHTAGS[hashtag])
|
|
send_photo.caption = hashtag
|
|
send_photo(token)
|
|
|
|
|
|
def parse_update(token: AnyStr, update: types.Update) -> int:
|
|
print(update)
|
|
print()
|
|
for i in ('message', 'edited_message', 'channel_post', 'edited_channel_post'):
|
|
if i in update:
|
|
parse_message(token, update[i])
|
|
break
|
|
return update.update_id
|
|
|
|
|
|
def on_updates(token: AnyStr, updates: Sequence[types.Update]) -> int:
|
|
max_id = 0
|
|
for u in updates:
|
|
try:
|
|
upd_id = parse_update(token, u)
|
|
|
|
if upd_id > max_id:
|
|
max_id = upd_id
|
|
except errors.TelegramError:
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
return max_id
|
|
|
|
|
|
def mainloop(token: AnyStr, offset: int = 0) -> int:
|
|
updates = methods.getUpdates(offset)(token)
|
|
return on_updates(token, updates)
|
|
|
|
|
|
def main():
|
|
token = sys.argv[1]
|
|
prev_offset = 0
|
|
|
|
try:
|
|
while True:
|
|
prev_offset = mainloop(token, prev_offset) + 1
|
|
except KeyboardInterrupt:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|