Welcome bot telegram python

lbellomo / a_simple_welcome_bot.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

»’A Simple Welcome Telegram Bot that gives simple (configurable) welcome messages»’
import os
import json
from telegram . ext import Updater , CommandHandler , MessageHandler , BaseFilter
# link to mwt file: https://gist.github.com/jh0ker/56f5b4fb7d015b1b9e4c74d4a91d4568
from mwt import MWT
def read_json ( path_json ):
»’Lee el json con los mensajes»’
with open ( path_json , ‘r’ ) as f :
new_dict = json . load ( f )
return new_dict
def save_json ( path_json , dict_object ):
»’Salva a disco los mensajes»’
with open ( path_json , ‘w’ ) as f :
json . dump ( dict_object , f )
# sacado de ‘code-snippers’:
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#get-the-add-group-message
class NewMember ( BaseFilter ):
def filter ( self , message ):
if not message . new_chat_members :
return False
return True
def get_group_message ( chat_id ):
»’Si existe devuelve el mensaje, sino el defaut mensaje»’
chat_id = str ( chat_id )
if chat_id not in msjs . keys ():
msj = defaut_msj
else :
msj = msjs [ chat_id ]
return msj
def add_group ( bot , update ):
chat_id = update . message . chat_id
msj = get_group_message ( chat_id )
for members in update . message . new_chat_members :
bot . send_message ( update . message . chat_id ,
text = msj )
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#cached-telegram-group-administrator-check
@ MWT ( timeout = 60 * 60 )
def get_admin_ids ( bot , chat_id ):
«»»Returns a list of admin IDs for a given chat. Results are cached for 1 hour.»»»
return [ admin . user . id for admin in bot . get_chat_administrators ( chat_id )]
def change_message ( bot , update ):
»’Guarda un nuevo mensaje, si tiene los permisos para hacerlo»’
chat_id = str ( update . message . chat_id )
all_admin = update . message . chat . all_members_are_administrators
is_a_admin = update . message . from_user . id in get_admin_ids ( bot , chat_id )
if all_admin or is_a_admin :
update_msj = update . message . text . split ( maxsplit = 1 )[ 1 ]
msjs [ chat_id ] = update_msj
save_json ( path_json , msjs )
bot . send_message ( chat_id = chat_id , text = «Welcome message update» )
else :
bot . send_message ( chat_id = chat_id , text = «You need be a admin to change the message» )
def print_message ( bot , update ):
»’Muestra el mensaje acutal del grupo»’
chat_id = update . message . chat_id
msj = get_group_message ( chat_id )
bot . send_message ( chat_id = chat_id , text = msj )
def help_message ( bot , update ):
»’Muestra una help basica»’
help_msj = »’Show welcome messages when a user join a group.
Only the group admin could change it.
Usage:
/change_message new_mesage — Change the welcome group message
/print_message — Display the current welcome group message
/help — Display this help message
Example:
/change_message My awesome new welcome message!!»’
bot . send_message ( chat_id = update . message . chat_id , text = help_msj )
with open ( ‘secret’ ) as f :
token = f . readline (). strip ()
path_json = ‘msjs.json’
if os . path . isfile ( path_json ):
msjs = read_json ( path_json )
else :
msjs = dict ()
defaut_msj = ‘Welcome!’
updater = Updater ( token = token )
dispatcher = updater . dispatcher
add_group_handle = MessageHandler ( callback = add_group , filters = NewMember ())
dispatcher . add_handler ( add_group_handle )
change_message_handler = CommandHandler ( ‘change_message’ , change_message )
dispatcher . add_handler ( change_message_handler )
print_message_handler = CommandHandler ( ‘print_message’ , print_message )
dispatcher . add_handler ( print_message_handler )
help_handler = CommandHandler ( ‘help’ , help_message )
dispatcher . add_handler ( help_handler )
updater . start_polling ()
Читайте также:  Preload all css images

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Telegram Python Bot. Welcomes everyone that enters a group chat that it’s part of.

License

jh0ker/welcomebot

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A Python Telegram Bot that greets everyone who joins a group chat

It uses the python-telegram-bot library and pickledb for basic persistence.

The file is prepared to be run by anyone by filling out the blanks in the configuration. The bot currently runs on @jh0ker_welcomebot

  • Clone the repo
  • Install the requirements from requirements.txt (using a virtualenv is recommended)
  • Edit TOKEN and BOTNAME in config.py
  • Follow Bot instructions

About

Telegram Python Bot. Welcomes everyone that enters a group chat that it’s part of.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A bot can receive new chat member then send the welcome message

License

KunoiSayami/WelcomeBot-Telegram

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Remember: Bot only receive administrators’ and owner’s command

Command Parameter(s) Description
/setwelcome (gist link | markdown text) Set welcome message
/clear N/A Clear setting welcome message
/ping N/A Return current session information
/setflag (flags) (1|0) Setting bot flags

Bot is now support placeholder, using them in welcome message, bot will auto-replace them to what you wish

Placeholder Replace to
$name user nickname

Known bug: If username contains markdown characters may cause markdown error (replace process not work)

Flag Description Default
no_welcome Bot will auto delete previous welcome message False
ignore_err Show rude message to no privilege member who using bot command True
no_blue While bot is admin, it will delete bot command after 5 seconds (need delete privilege) False
no_new_member Bot will auto delete `Joined group’ message (system generated) (need delete privilege) False
no_service_msg Bot will auto delete service message False

Set welcome message to Welcome [who] to my group:

/setwelcome **Welcome $name to my group** 
/setwelcome https://gist.githubusercontent.com/anonymous/6446fbae52916bc7fb092dd1ee3f8483/raw/4ad5231d5e2a68458e117db9bed97407dfe6f47b/welcomemsg 

After you run this command, bot will reply your a message

In principle, need python 3.7.x interpreter and PostgreSQL database

The following libraries are required:

  • Copy data/config.ini.default to data/config.ini
  • Change the database configure and telegram bot token in config.ini
  • Import group_welcome.sql to database which you will connect

After configure, you can use ./welcome_bot.py to run this bot

You should run ./welcome_bot.py upgrade after updated code, that’s all.

To clone repo, please use the following code

git clone https://github.com/KunoiSayami/WelcomeBot-Telegram.git $TargetFolder cd $TargetFolder git submodule update --init

To pull repo, please use the following code

git pull git submodule update

If bot is group admin, other member who not admin or owner using command will receive 1 minute cold down (need ban user privilege)

Copyright (C) 2017-2022 KunoiSayami

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

About

A bot can receive new chat member then send the welcome message

Источник

Оцените статью