init
This commit is contained in:
52
tgbot/rest.py
Normal file
52
tgbot/rest.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
TOKEN = os.environ.get('BOT_TOKEN')
|
||||
apiBase = f"https://api.telegram.org/bot{TOKEN}/"
|
||||
|
||||
|
||||
def register_webhook(url):
|
||||
return requests.get(
|
||||
apiBase + f'setWebhook?url={url}'
|
||||
)
|
||||
|
||||
|
||||
def delete_message(cid, mid):
|
||||
return requests.post(
|
||||
apiBase + f"deleteMessage?chat_id={cid}&message_id={mid}"
|
||||
)
|
||||
|
||||
|
||||
def send_message(cid, body, reply_to=None, reply_markup=None):
|
||||
url = apiBase + f"sendMessage?chat_id={cid}&text={body}"
|
||||
if reply_to:
|
||||
url += f'&reply_to_message_id={reply_to}'
|
||||
if reply_markup:
|
||||
reply_markup = json.dumps(reply_markup)
|
||||
reply_markup = requests.utils.quote(reply_markup)
|
||||
url += f'&reply_markup={reply_markup}'
|
||||
r = requests.post(url)
|
||||
return r
|
||||
|
||||
|
||||
# https://core.telegram.org/bots/api#banchatmember
|
||||
def ban_member(chat_id, user_id, until_date=None):
|
||||
url = apiBase + f"banChatMember?chat_id={chat_id}&user_id={user_id}"
|
||||
if until_date:
|
||||
url += f'&until_data={until_date}'
|
||||
r = requests.post(url)
|
||||
return r
|
||||
|
||||
# https://core.telegram.org/bots/api#unbanchatmember
|
||||
def unban_member(chat_id, user_id):
|
||||
url = apiBase + f"unbanChatMember?chat_id={chat_id}&user_id={user_id}&only_if_banned=1"
|
||||
r = requests.post(url)
|
||||
return r
|
||||
|
||||
|
||||
def forward_message(cid, mid, to_chat_id):
|
||||
url = apiBase + f"forwardMessage?chat_id={to_chat_id}" + \
|
||||
f"&from_chat_id={cid}&message_id={mid}"
|
||||
r = requests.post(url)
|
||||
return r
|
Reference in New Issue
Block a user