Skip to content
Open

chat #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion telegram/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .bot import Bot
from .bot import Bot
from .update import Update
from .message import Message
from .user import User
36 changes: 17 additions & 19 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def getMe(self)->User:

return User(user_data)


def sendMessage(self,chat_id,text):
"""Use this method to send text messages.
Args:
Expand All @@ -31,28 +30,22 @@ def sendMessage(self,chat_id,text):
A telegram.Message instance representing the message posted.
"""

url = self.base_url+"/sendMessage"
url = f"{self.base_url}/sendMessage"
data={'chat_id': chat_id, 'text': text}
answer = requests.post(url,data)
return answer.json()




def getUpdates(self):
"""Use this method to receive incoming updates using long polling.
Args:

Returns:
A telegram.Update object is returned.
"""


url = f'{self.base_url}/getUpdates'

answer = requests.get(url)
data = answer.json()

# Get result form data
result = data['result']
updates = []
Expand All @@ -61,7 +54,7 @@ def getUpdates(self):
updates.append(Update(update))

return updates

def sendPhoto(self,
chat_id,
photo,
Expand All @@ -88,11 +81,10 @@ def sendPhoto(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendPhoto"
url = f"{self.base_url}/sendPhoto"
data={'chat_id': chat_id,'photo':photo}
answer = requests.post(url,data)
return answer.json()


def sendSticker(self,
chat_id,
Expand All @@ -116,7 +108,7 @@ def sendSticker(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendSticker"
url = f"{self.base_url}/sendSticker"
data={'chat_id': chat_id, 'sticker':sticker}
answer = requests.post(url,data)
return answer.json()
Expand Down Expand Up @@ -144,7 +136,7 @@ def sendLocation(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendLocation"
url = f"{self.base_url}/sendLocation"
data={'chat_id': chat_id, 'latitude':latitude, 'longitude':longitude}
answer = requests.post(url,data)
return answer.json()
Expand All @@ -171,11 +163,11 @@ def sendDocument(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendDocument"
url = f"{self.base_url}/sendDocument"
data={'chat_id': chat_id, 'document':document}
answer = requests.post(url,data)
return answer.json()

def sendAudio(self,
chat_id,
audio,
Expand All @@ -201,12 +193,11 @@ def sendAudio(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendAudio"
url = f"{self.base_url}/sendAudio"
data={'chat_id': chat_id, 'audio':audio}
answer = requests.post(url,data)
return answer.json()


def sendVideo(self,
chat_id,
video,
Expand All @@ -230,7 +221,14 @@ def sendVideo(self,
Returns:
A telegram.Message instance representing the message posted.
"""
url = self.base_url+"/sendVideo"
url = f"{self.base_url}/sendVideo"
data={'chat_id': chat_id, 'video':video}
answer = requests.post(url,data)
return answer.json()

def sendContact(self, chat_id, phone_number, first_name):
url = f'{self.base_url}/sendContact'
p = {'chat_id':chat_id, 'phone_number':phone_number, 'first_name':first_name}
r = requests.get(url, params=p)

return r.json()
2 changes: 1 addition & 1 deletion telegram/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def fromDict(self) -> dict:
}
newdict = {}
for key, value in chat_dict.items():
if value:
if value != None:
newdict[key] = value

return newdict
Expand Down
17 changes: 16 additions & 1 deletion telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ def fromDict(self)->dict:
Returns:
dict: dictionary of user data
'''
if len(self.base_message) > 0:
data_messag = self.base_message[-1]['message']['from']
data = {
'id':data_messag.get('id'),
'last_name':data_messag.get('last_name'),
'first_name':data_messag.get('first_name'),
'username':data_messag.get('username'),
'is_bot':data_messag.get('is_bot'),
"language_code":data_messag.get('language_code'),
'is_premium':data_messag.get('is_premium'),
'added_to_attachment_menu':data_messag.get('added_to_attachment_menu'),
'can_join_groups':data_messag.get('can_join_groups'),
'can_read_all_group_messages':data_messag.get('can_read_all_group_messages'),
'supports_inline_queries':data_messag.get('supports_inline_queries'),
}

msg_dict = {
'message_id': self.message_id,
'from_user': self.from_user.fromDict(),
Expand All @@ -26,4 +42,3 @@ def __str__(self):
Print the user data
'''
return json.dumps(self.fromDict())

2 changes: 0 additions & 2 deletions telegram/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def __init__(self, update) -> None:
self.update_id = update['update_id']
self.message = Message(update['message'])


def fromDict(self)->dict:
'''
Convert User object to dictionary
Expand All @@ -24,4 +23,3 @@ def __str__(self):
Print the user data
'''
return json.dumps(self.fromDict())

2 changes: 0 additions & 2 deletions telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ def __str__(self):
Print the user data
'''
return json.dumps(self.fromDict())