-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegrambots.py
More file actions
173 lines (133 loc) · 4.87 KB
/
telegrambots.py
File metadata and controls
173 lines (133 loc) · 4.87 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""telegrambots.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1m8sk-xzO5sZ3hnVpboI-qrKzEy1jsYhv
# Starting using telegram bots
getting messages
"""
import requests
resp = requests.get("https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/getUpdates")
print(resp.text)
"""# Second part of getting messages
Reading data from group using parameters
"""
import requests
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/getUpdates"
parameters = {
"offset": "367086623", #update id to start from
"limit": "1" #number of messages to show
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
"""# Sending Messages
Using the sendMessage endpoint
"""
import requests
import time
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendMessage"
jokes=['Did you hear about the mathematician who’s afraid of negative numbers? He’ll stop at nothing to avoid them.',
'Why do we tell actors to “break a leg?”Because every play has a cast. Here are some dark jokes to check out if you have a morbid sense of humor.',
'Helvetica and Times New Roman walk into a bar.“Get out of here!” shouts the bartender. “We don’t serve your type.”',
'Once my dog ate all the Scrabble tiles. For days he kept leaving little messages around the house. Don’t miss these hilarious egg puns that will absolutely crack you up.',
'Yesterday I saw a guy spill all his Scrabble letters on the road. I asked him, “What’s the word on the street?”',
'Once my dog ate all the Scrabble tiles. For days he kept leaving little messages around the house. Don’t miss these hilarious egg puns that will absolutely crack you up.'
]
for joke in jokes:
parameters = {
"chat_id": "-743974581", #specific chat id
"text": joke #text message to send
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
time.sleep(15)
"""# Sending Photos
sending images
"""
import requests
import time
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendPhoto"
images=['https://comparables.co.ke/static/blog/probability.png'
]
for image in images:
parameters = {
"chat_id": "-743974581", #specific chat id
"photo": image, #image to send
"caption": "Here is a test image from comparables website"
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
time.sleep(15)
"""# Sending Audio files"""
import requests
import time
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendAudio"
audios=['https://www.bensound.com/bensound-music/bensound-ukulele.mp3',
'https://www.bensound.com/bensound-music/bensound-anewbeginning.mp3',
'https://www.bensound.com/bensound-music/bensound-littleidea.mp3'
]
#to send from a local computer just change the location to correspond to file navigation location, P.S. it may take some time to upgrade
for audio in audios:
parameters = {
"chat_id": "-743974581", #specific chat id
"audio": audio, #image to send
"caption": "Here is an audio file from bensound"
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
time.sleep(15)
"""# sending any file from a local computer
<pdf, csv, doc, etc>
"""
import requests
import time
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendDocument"
documents=['/content/sample_data/mnist_test.csv',
'/content/sample_data/mnist_train_small.csv'
]
for document in documents:
parameters = {
"chat_id": "-743974581", #specific chat id
"caption": "Here is a file from drive/local"
}
files ={
"document":document
}
resp=requests.get(base_url, data=parameters, files=files)
print(resp.text)
time.sleep(15)
"""# Creating Quizzes
Telegram Polls
"""
import requests
import time
import json
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendPoll"
#for image in images:
parameters = {
"chat_id": "-743974581", #specific chat id
"question": "Are you interested in bitcons or Ethereum?", #question to send
"options": json.dumps(['Bitcons', 'Ethereum']),
"is_anonymous": False
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
time.sleep(15)
"""# Telegram Quiz"""
import requests
import time
import json
base_url="https://api.telegram.org/bot5198206552:AAG6-73A47O0GnzNJcLWkG57OD8ICeV6eGU/sendPoll"
#for image in images:
parameters = {
"chat_id": "-743974581", #specific chat id
"question": "How much is 100+200", #question to send
"options": json.dumps(['1200', '300', '500', '2000']),
"is_anonymous": False,
"type":"quiz",
"correct_option_id":1,
"explanation": "learn some maths"
}
resp=requests.get(base_url, data=parameters)
print(resp.text)
time.sleep(15)