forked from chen310/NeteaseCloudMusicTasks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Telegram.py
28 lines (25 loc) · 1.04 KB
/
Telegram.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
# -*- coding: utf-8 -*-
import requests
import json
def getKey(data):
config = data['config']
if len(config['userId']) == 0 or len(config['botToken']) == 0:
return None
return (config['module'], config['userId'], config['botToken'])
def push(title, mdmsg, mdmsg_compat, textmsg, config):
#deprecate str.replace() funciton since Telegram support ** syntax to bold text.
msg = mdmsg_compat # .replace('**', '*')
if len(config['userId']) == 0 or len(config['botToken']) == 0:
return
server = config['server']
if server:
if server.endswith('/'):
server = server[:-1]
else:
server = 'https://api.telegram.org'
url = server + '/bot' + config['botToken'] + '/sendMessage'
ret = requests.post(url, data={'chat_id': config['userId'], 'text': msg, 'parse_mode': "Markdown"}, headers={
'Content-Type': 'application/x-www-form-urlencoded'})
print('Telegram response: \n', ret.status_code)
if ret.status_code != 200:
print(ret.content.decode('utf-8'))