Skip to content

Instantly share code, notes, and snippets.

@VycktorStark
Last active July 4, 2020 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save VycktorStark/326ec526b13cbfbb8270071bffc4724e to your computer and use it in GitHub Desktop.
Save VycktorStark/326ec526b13cbfbb8270071bffc4724e to your computer and use it in GitHub Desktop.
This method will help you to manipulate the Telegram API easily
__author__ = "Vycktor Stark"
class Method():
"""
This method will help you to manipulate the Telegram API easily,
because basically it will communicate with the api by sending
the necessary arguments without the need to write a lot of code
or use a Telegram Frameworks / SDK / Wrapper.
"""
def __init__(self):
import os
self.token = os.environ['SECRET_KEY'] #Setando token do bot
self.apitelegram = f"https://api.telegram.org/bot{self.token}" #Setando api-telegram-bot
self.headers = {'content-type': 'application/json', 'Cache-Control': 'no-cache'} #Setando headers
@staticmethod
def sendRequest(url, params=None, headers=None, files=False, post=False):
import requests, json
"""
This function, when called, will execute a request at the URL
provided with the defined arguments.
"""
try:
if (post):
data = requests.post(url, params=params, headers=headers, files=files, post=post)
else:
data = requests.get(url, params=params, headers=headers, files=files)
except Exception as error:
print(error); data = False
if (data != False):
if (data.status_code == 200):
return dict(success=True, code=data.status_code, response=data.json())
else:
return dict(success=False, code=data.status_code, response=data.json())
else:
return dict(success=False, code=404, response=False)
def sendTG(self, method="sendMessage", strfile=False, file=False, **args):
"""
This function, when called, executes a request that
sends arguments to the API-Telegram-BOT.
"""
if (strfile !=False) and (file !=False):
return self.sendRequest(f"{self.apitelegram}/{method}", params=locals()['args'], headers=self.headers, files=dict(strfile=file), post=True)
elif (strfile == False) and (file == False):
return self.sendRequest(f"{self.apitelegram}/{method}", params=locals()['args'], headers=self.headers)
"""
Example of how to use:
from name_file import Method
api = Method()
api.sendTG(method="getME")
api.sendTG(chat_id=438131290,text='oi')
api.sendTG(method="sendPhoto", chat_id=438131290, photo="https://img.olhardigital.com.br/uploads/acervo_imagens/2020/04/r4x3/20200423030657_660_495_-_python.jpg", caption='<b>ping</b>', parse_mode='HTML')
Another example:
from name_file import Method
import os, gtts
api = Method()
try:
AUDIO = gtts.gTTS(cmd[1], lang="en")
AUDIO.save('audio.ogg')
AUDIO = open('audio.ogg', 'rb')
api.sendTG(method="sendAudio", strfile="audio", file=AUDIO, chat_id=438131290)
except Exception as error:
print(error)
finally:
AUDIO.close()
os.remove('audio.ogg')
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment