├── LICENSE ├── README.md ├── asdf.png ├── de.png └── telegram_bot.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Abhineet Raj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram-bot API (in Python) 2 | 3 | 4 | * This library provides a pure Python, asynchronous interface for the Telegram Bot API. It's compatible with Python versions 3.7+. 5 | * Made for Auto replies in group. 6 | * If you are using ServeBot function , better deploy it into linux VPS server. 7 | ## Installation 8 | * Install python3.8 9 | * Downnload this project 10 | 11 | ## Guide 12 | 13 | ### For sending messages in group 14 | 15 | ``` 16 | from telegram_bot import* 17 | Bot.set(api) #Get your api from BotFather 18 | Bot.sendMessage("Hello World",chat_id) #Get the chatID of the required group 19 | ``` 20 | 21 | ### For sending recieving commands in group 22 | 23 | ``` 24 | from telegram_bot import* 25 | Bot.set(api) #Get your api from BotFather 26 | Bot.getMessages(chat_id) #Get the chat Id of the required group 27 | ``` 28 | 29 | If someone sends /hello , you will recieve "/hello" value 30 | 31 | ### Creating an auto reply server 32 | 33 | ``` 34 | from telegram_bot import* 35 | Bot.set(api) #Get your api from BotFather 36 | 37 | ServeBot.command(chat_id, command, reply) # Get the chat_id of the group 38 | ServeBot.run() 39 | ``` 40 | 41 | #### Example 42 | 43 | * Code 44 | ``` 45 | from telegram_bot import* 46 | Bot.set("5681058372:AAE6VWdgxWLQqKHvmY6ceJw0lEy20B-Vjjg") 47 | ServeBot.command("-704414602","/start","Hii, I am testbot") 48 | ServeBot.command("-704414602","/about","This bot is made in Python3.8 by Abhineet Raj - @abhineetraj1") 49 | ServeBot.run() 50 | ``` 51 | 52 | 53 | 54 |
55 | 56 | #### Note:- 57 | The api and chat id used in example is expired. 58 | ## Programming languages used 59 | Python 60 | -------------------------------------------------------------------------------- /asdf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhineetraj1/telegram-bot-python/34b9137be21bf6d62193f240f85f065fc870bc23/asdf.png -------------------------------------------------------------------------------- /de.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhineetraj1/telegram-bot-python/34b9137be21bf6d62193f240f85f065fc870bc23/de.png -------------------------------------------------------------------------------- /telegram_bot.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from time import sleep 3 | set_token, line_cmd, response, chat_sec, history = [],[],[],[],[0] 4 | 5 | #Creating Bot to set API keys , send and recieve message from group 6 | class Bot: 7 | def set(api_key): 8 | try: 9 | #parameters = {"offset":chat_ID,"limit":"30"} 10 | req = requests.get("https://api.telegram.org/bot"+api_key+"/getUpdates") 11 | set_token.append(api_key) 12 | return "API is set" 13 | except: 14 | print("Enter correct api key") 15 | def sendMessage(text, chat_ID): 16 | idf = [(len(set_token) == 1), isinstance(text,str),isinstance(chat_ID,str)] 17 | if (False in idf): 18 | return "Error\nEnter the right format" 19 | else: 20 | parameters = {"chat_id":chat_ID,"text":text} 21 | req = requests.get("https://api.telegram.org/bot"+set_token[0]+"/sendMessage", params=parameters) 22 | return "Sent!" 23 | def getMessage(chat_ID): 24 | if (isinstance(chat_ID,str)): 25 | parameters = {"offset":chat_ID} 26 | a = requests.get("https://api.telegram.org/bot"+set_token[0]+"/getUpdates", data = parameters).json() 27 | return a["result"][len(a["result"])-1]["message"]["text"] 28 | else: 29 | return "Chat ID should be string" 30 | 31 | #Creating class to steadily run bot in telegram group 32 | class ServeBot: 33 | def command(chat_ID, res, text): 34 | if (False in [isinstance(chat_ID,str), isinstance(res, str), isinstance(text, str), ("/" in res)]): 35 | return "Error\nEnter correct parameters" 36 | else: 37 | chat_sec.append(chat_ID) 38 | line_cmd.append(res) 39 | response.append(text) 40 | def run(): 41 | print("Bot is running!") 42 | while True: 43 | m=Bot.getMessage(chat_sec[0]) 44 | if (m in line_cmd): 45 | if(history[len(history)-1] == m): 46 | sleep(1) 47 | else: 48 | history.append(m) 49 | Bot.sendMessage(response[line_cmd.index(m)], chat_sec[0]) 50 | sleep(1) 51 | else: 52 | sleep(1) 53 | --------------------------------------------------------------------------------