-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.py
More file actions
137 lines (126 loc) · 5.43 KB
/
messages.py
File metadata and controls
137 lines (126 loc) · 5.43 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import requests
from request_types import RequestType
class Messages:
def __init__(self, token, chat_id, reply_keyboard=None, inline_keyboard=None, form=None,
text=None, file=None, file_data=None, contact=None, location=None):
self.header = {'token': str(token)}
self.urls = {'send_message': 'https://api.gap.im/sendMessage',
'action': 'https://api.gap.im/sendAction',
'file_upload': 'https://api.gap.im/upload',
'edit_message': 'https://api.gap.im/editMessage',
'delete_message': 'https://api.gap.im/deleteMessage',
'answer_callback': 'https://api.gap.im/answerCallback',
'send_invoice': 'https://api.gap.im/invoice',
'invoice_inquiry': 'https://api.gap.im/invoice/inquery',
'verify_payment': 'https://api.gap.im/payment/verify',
'payment_inquiry': 'https://api.gap.im/payment/inquery'}
self.chat_id = chat_id
self.reply_keyboard = reply_keyboard
self.inline_keyboard = inline_keyboard
self.form = form
self.message_text = text
self.file = file # file name or address on disk
self.file_data = file_data # type of Gap upload return json
self.contact = contact
self.location = location
def send_message_json_data_maker(self, message_type):
if message_type == RequestType['text_message'].value:
return self.message_text
elif (message_type == RequestType['image_message'].value
or message_type == RequestType['audio_message'].value
or message_type == RequestType['video_message'].value
or message_type == RequestType['receive_file'].value
or message_type == RequestType['voice_message'].value):
return self.file_data
elif message_type == RequestType['contact_message'].value:
return self.contact
elif message_type == RequestType['location_message'].value:
return self.location
elif message_type == 'action':
return None
def send_message_json_maker(self, message_type):
data_dict = {'chat_id': self.chat_id,
'type': message_type,
'data': self.send_message_json_data_maker(message_type),
'reply_keyboard': self.reply_keyboard,
'inline_keyboard': self.inline_keyboard,
'form': self.form}
return data_dict
def send_message(self, message_type):
"""this method can send these types of messages:
- text message
- image message
- video message
- audio message
- voice message
- file
- contact
- location"""
r = requests.post(url=self.urls['send_message'],
data=self.send_message_json_maker(message_type),
headers=self.header)
if r.status_code == 200:
return r.json()
else:
r.raise_for_status()
def upload_file(self, file_type):
file = {str(file_type): open(self.file, 'rb')}
r = requests.post(url=self.urls['file_upload'], file=file, headers=self.header)
if r.status_code == 200:
return r.json()
else:
r.raise_for_status()
def send_action(self):
r = requests.post(url=self.urls['action'],
data=self.send_message_json_maker('action'),
headers=self.header)
if not r:
r.raise_for_status()
def answer_callback(self, callback_id, text, show_alert=False):
send_dict = {'chat_id': self.chat_id,
'callback_id': callback_id,
'text': text,
'show_alert': show_alert}
r = requests.post(url=self.urls['answer_callback'],
data=send_dict,
headers=self.header)
if not r:
r.raise_for_status()
def send_invoice(self, amount, description):
send_dict = {'chat_id': self.chat_id,
'amount': amount,
'description': description}
r = requests.post(url=self.urls['send_invoice'],
data=send_dict,
headers=self.header)
if r:
return r.json()['id']
else:
r.raise_for_status()
def invoice_inquiry(self, ref_id):
send_dict = {'chat_id': self.chat_id,
'ref_id': ref_id}
r = requests.post(url=self.urls['invoice_inquiry'],
data=send_dict,
headers=self.header)
if r:
if r.json()['status'] == 'error':
return 'error'
elif r.json()['status'] == 'verified':
return r.json()
else:
r.raise_for_status()
def payment_inquiry(self, ref_id):
send_dict = {'chat_id': self.chat_id,
'ref_id': ref_id}
r = requests.post(url=self.urls['payment_inquiry'],
data=send_dict,
headers=self.header)
if r:
if r.json()['status'] == 'error':
return 'error'
elif r.json()['status'] == 'verified':
return r.json()
else:
r.raise_for_status()