Skip to Content

So, a lot of buzz going around Telegram Messenger, Bots, Telegram channels and other result of technological progress. Also no one can deny that it had become part of life all of us.

Well, we’ll try practical use of Telegram Bot technology in SAP Context.

Let’t try to solve “real world SAP problem” – create self service for Reset User password.

In general you have two possible options of integration with Telegram bot:

In simple cases (for test for example) you may use second options. But in more real world complex example we must realized the first one.

Toolbox using for this example:

General steps:

Create new bot via bot BotFatherAnd received a token your bot:Going Going to some coding in python:

import telebot
import mymodule
bot = telebot.TeleBot("YOUR TOKEN")
commands = {  # Just commands
              'start': 'Start working with bot',
              'help': 'Print help',
              'resetpass': 'Reset password in SAP'
}
@bot.message_handler(commands=['start'])
def send_welcome(message):
    msg = bot.send_message(message.chat.id, 'Welcome to Help Desk !')
    help_text = "Following commands are availiable: \n"
    for key in commands:  
        help_text += "/" + key + ": "
        help_text += commands[key] + "\n"
    bot.send_message(message.chat.id, help_text)
    
@bot.message_handler(commands=['resetpass'])
def send_welcome(message):
    msg = bot.reply_to(message, "Please enter user name") 
    bot.register_next_step_handler(msg, process_username_for_reset)
    
def process_username_for_reset(message):
    try:
        chat_id = message.chat.id
        user_name = message.text
        if user_name not in mymodule.get_user_list():
            bot.send_message(message.chat.id, 'User does not exist !')
        else:            
            mymodule.user_reset_pwd(user_name)
            bot.send_message(message.chat.id, 'Pasword reset, new password NewPassword1!')
    except Exception as e:
        bot.reply_to(message, 'Something wrong ')
    
@bot.message_handler(content_types=["text"])
def unavailiable_command(message): 
    msg = bot.send_message(message.chat.id, 'This command does not exist')
    
if __name__ == '__main__':
    bot.polling(none_stop=True)

Create config file sapnwrfc.cfg for sap connection:

[connection]
# sap system ip
ashost = XXXXXXXXXXXXX
# sap client id
client = XXX
# sap system number
sysnr = XX
# sap username
user = SERVICEUSER
# sap password
passwd = XXXXXXXX

Create two simple function get_user_list and user_reset_pwd:

from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError
from ConfigParser import ConfigParser

def get_user_list():
   
    try:
        config = ConfigParser()
        config.read('sapnwrfc.cfg')
        params_connection = config._sections['connection']
        conn = Connection(**params_connection)
        result = conn.call('BAPI_USER_GETLIST')
        user_list = result["USERLIST"]
        return [x['USERNAME'] for x in user_list]
        
    except CommunicationError:
        print u"Could not connect to server."
        raise
    except LogonError:
        print u"Could not log in. Wrong credentials?"
        raise
    except (ABAPApplicationError, ABAPRuntimeError):
        print u"An error occurred."
        raise

def user_reset_pwd(user_name):
   
    try:
        str_newpass = {'BAPIPWD':'NewPassword1!'}
        str_newpassx = {'BAPIPWD':'X'}
        config = ConfigParser()
        config.read('sapnwrfc.cfg')
        params_connection = config._sections['connection']
        conn = Connection(**params_connection)
        result = conn.call('BAPI_USER_CHANGE',USERNAME = user_name, PASSWORD = str_newpass, PASSWORDX = str_newpassx )
        
    except CommunicationError:
        print u"Could not connect to server."
        raise
    except LogonError:
        print u"Could not log in. Wrong credentials?"
        raise
    except (ABAPApplicationError, ABAPRuntimeError):
        print u"An error occurred."
        raise

Execute and test:

Here we go !. Check the result with TESTUSER1:

 

Small comments:

Above r&d does not  out of the box solution and required fine-tune for real system.

In my real solution for real customer the following points was solved and released :

Security point 1 (Check Telephone number  of user who ask the reset and telephone number in SAP User profile and approved only if equal )

Security point 2 (Limited authorization for RFC service user   )

Function point 3  (Some another possible bot command in solution like ask for new role, block, unblock user )

Just for help:

Python: https://www.python.org/

Telegram Api: https://github.com/eternnoir/pyTelegramBotAPI

pyRFC: http://sap.github.io/PyRFC/install.html

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply