Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python telegram bot

$ pip install python-telegram-bot --upgrade
Comment

python telegram bot login

import telebot
import time

bot = telebot.TeleBot('TOKEN')

def extract_unique_code(text):
    # Extracts the unique_code from the sent /start command.
    return text.split()[1] if len(text.split()) > 1 else None

def in_storage(unique_code): 
    # Should check if a unique code exists in storage
    return True

def get_username_from_storage(unique_code): 
    # Does a query to the storage, retrieving the associated username
    # Should be replaced by a real database-lookup.
    return "ABC" if in_storage(unique_code) else None

def save_chat_id(chat_id, username):
    # Save the chat_id->username to storage
    # Should be replaced by a real database query.
    pass

@bot.message_handler(commands=['start'])
def send_welcome(message):
    unique_code = extract_unique_code(message.text)
    if unique_code: # if the '/start' command contains a unique_code
        username = get_username_from_storage(unique_code)
        if username: # if the username exists in our database
            save_chat_id(message.chat.id, username)
            reply = "Hello {0}, how are you?".format(username)
        else:
            reply = "I have no clue who you are..."
    else:
        reply = "Please visit me via a provided URL from the website."
    bot.reply_to(message, reply)

bot.polling()

while True:
    time.sleep(0)
Comment

PREVIOUS NEXT
Code Example
Python :: python if true 
Python :: List comprehension if-else 
Python :: extract coordinate values in xarray 
Python :: tkinter frames and grids 
Python :: how to drop duplicate columns in pandas that dont have the same name? 
Python :: super in django manager 
Python :: re python3 
Python :: request download file 
Python :: get xlim python 
Python :: palindrome checker python 
Python :: using Decorators 
Python :: pandas groupby most frequent 
Python :: convert python script to exe 
Python :: Difference between two dates and times in python 
Python :: python create random mac 
Python :: df index drop duplicates 
Python :: how to write a python comment 
Python :: cv2 frame size 
Python :: python min key 
Python :: python to postgresql 
Python :: python os get dir path 
Python :: merge keep left index 
Python :: python -c 
Python :: how to add a column with more rows to a dataframe 
Python :: add timestamp csv python 
Python :: django render example 
Python :: from pandas to dictionary 
Python :: python recognize every white color 
Python :: convert sentence to words python 
Python :: how to add value in array django 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =