Skip to content

Instantly share code, notes, and snippets.

@rahul-yr
Created September 6, 2022 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul-yr/3de0d526c5a94981b7bef67e3c24338a to your computer and use it in GitHub Desktop.
Save rahul-yr/3de0d526c5a94981b7bef67e3c24338a to your computer and use it in GitHub Desktop.
Telegram Python bot snippets for getUpdates and InlineKeyboard
import requests
import json
def getUpdates():
api = "https://api.telegram.org/bot<token>/getUpdates"
# add headers
headers = {'Content-Type': 'application/json'}
# add body params
data = {'allowed_updates': ['update_id']}
# stringify body params
data = json.dumps(data)
# send request
response = requests.post(api, headers=headers, data=data)
# print response
print(response.text)
def sendMessageWithInlineButtons():
api = "https://api.telegram.org/bot<token>/sendMessage"
# add headers
headers = {'Content-Type': 'application/json'}
data = {
'chat_id': '<recipent_id>', 'text': 'Hello World',
'reply_markup': {
'keyboard': [
[{'text': 'Button 1'},
{'text': 'Button 2'}]],
'resize_keyboard': True,
'one_time_keyboard': True
}}
# stringify body params
data = json.dumps(data)
# send request
response = requests.post(api, headers=headers, data=data)
# print response
print(response.text)
if __name__ == '__main__':
getUpdates()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment