-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgme.py
executable file
·54 lines (45 loc) · 1.96 KB
/
tgme.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
#!/usr/bin/env python3
# author: vort3
# inspired by: https://github.com/marcotrosi/tgme
import urllib.request as request
import urllib.parse as parse
import sys
import os
def main():
if len(sys.argv) == 1:
print(""" NAME:
tgme - send messages to your Telegram account
SYNOPSIS:
tgme "TEXT"
tgme "TITLE" "TEXT"
tgme "TITLE" "SUBTITLE" "TEXT"
CONFIGURATION:
The configuration shall consist of two files.
One file named "token" shall contain the Telegram API token of your bot.
A second file named "chatid" that shall contain the Telegram chat ID in which the bot is added.
Use botfather within Telegram to create a bot and get a token for it.
Add the bot and send a dummy message, then put "https://api.telegram.org/bot<YourBOTToken>/getUpdates"
in your browser to find the chat_id.""")
return
if '-' not in sys.argv:
if len(sys.argv) == 2: message = sys.argv[1]
elif len(sys.argv) == 3: message = f"<b>{sys.argv[1]}</b>\n\n{sys.argv[2]}"
else: message = f"<b>{sys.argv[1]}</b>\n<i>{sys.argv[2]}</i>\n\n{' '.join(sys.argv[3:])}"
else:
message = "".join(sys.stdin.readlines())
message = message.replace("&", "&")
message = message.replace("<", "<")
message = message.replace(">", ">")
if message == "":
return
message = "<code>" + message + "</code>"
configpath = os.path.join(os.path.expanduser("~"), ".config", "tgme")
with open(os.path.join(configpath, "token")) as tokenfile:
token = tokenfile.read().strip()
with open(os.path.join(configpath, "chatid")) as chatidfile:
chatid = chatidfile.read().strip()
URL = f"https://api.telegram.org/bot{token}/sendMessage"
data = parse.urlencode({"chat_id" : chatid, "text" : message, "parse_mode" : "HTML"})
request.urlopen(f"{URL}?{data}")
if __name__ == "__main__":
main()