Slack hooks - Post an interactive command - python

How to post /poll command to Slack using hooks correctly? When we use it like this:
URL = 'https://hooks.slack.com/services/XXX/XXX/XXX'
CMD = '/poll "Name" "A" "B"'
requests.post(URL, json = {'text': CMD})
The result in slack is just text, not the applied command.

It's not possible for a slash command to be invoked by another app or integration. Slash commands are interpreted first by a Slack client and then executed -- human interaction is expected. You won't be able to execute a slash command by sending text via a webhook.

Related

Python to post and format API response data into Slack channel

Working on a script to present data in Slack channel.. I have a script I'm working on to request and return data that I need to post in the channel that gets invoked with a Slash command, but having an issue with presenting the data in the slack channel from where I've executed the Slash command. I've been attempting to work with the Block Kit Builder, but I see no way of presenting that data coming back from my script using the template I created.
Then in the Block kit builder, I can kind of see the format I want and send it to Slack from the Block kit builder, but if I wanted my return response from my Python script to be formatted by the template and respond in the channel, it doesn't seem to work.. I'm thinking I'm definitely doing something wrong, but was looking for suggestions..
I've been searching on possibly how to do it in Flask, which is what I'm using to execute a Slash command in Slack to invoke my Python script to query the API, get the response and send it to Slack..
Thanx.
P.S. Stackflow won't allow me to post a snippet of the json and/or images.. yet... something about a '10 reputation'...
I was able to figure this out. In my code, I reflected this:
for i in pdjson:
t = {}
try:
t["text"] = {
"type": "plain_text",
"text": f'{i["escalation_policy"]["summary"]} - Layer: {i["escalation_level"]}'
}
except (KeyError):
t["text"] = {
"type": "plain_text",
"text": f'{i["escalation_policy"]["summary"]} - Layer: {i["escalation_level"]}'
}
t["value"] = i["user"]["summary"]
Now I can present in the slack workspace with the block kit template. Now I just have to figure out how make the 'value' show up once I select an item in the list.

How to send python output to telegram CHANNEL not to Group and gmail email group

Hi I have a program in python that generates results every one hour. The result can be of anything.This program will run in local machine or in the virtual private network.
I have two requirements
1. Send this python generated result to one telegram group [Group name "ourworld"](created by me) automatically without user intervention . (I have desktop telegram client running or web.telegram.org running in the same system)
Send this result to gmail group email ID.
what are the methods available to achieve this requirement .Is there any working code available to do this job .Please share the info and details.
Edit:
The Issue that i am facing :
1.created a Bot using BotFather.
2.Adding this Bot to my Group ,Here i get an error could not add an member So added the Bot as admin in the group
3.Token of the BOT noted down.
4. Trying to get ChatId
in this forum (https://web.telegram.org/#/im?p=g154513121) someone says number after p=g is the chartid ,In my case there is no
number it shows #testingname like this.
Using this approach trying to get the Chat ID https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/getme
so here 738909732 become a chat Id (in this case why we need seperate
call for the chart id)
here it is true as response coming! Here the chat id is the ID of the my "testingname" no chart id generated for the group.
6.Now packing the URL to see the response i am getting this error.
https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/sendMessage?chat_id=738909732&text=testing
the output if i run this in browser
{"ok":false,"error_code":400,"description":"Bad Request: chat not
found"} {"ok":false,"error_code":403,"description":"Forbidden: bot
can't send messages to bots"}
How to resolve this issue and make the BOT working .why i am not able to add BOT to my group that says error "Cant Add user"
How to make this telegram group working.
Note : Using BotFather BOT created
In case for sending message to telegram group the above method provided by bipin_s works where the chat_id = -xxxxxx is used.This is correct id followed by - symbol to be used.
For Posting message in the "telegram channel " a minor change needs to be done in the URL.The URL must be framed as below .
url = "https://api.telegram.org/botTokenID/sendMessage?chat_id=#yourChannelChatID&text=message"
Replace the TokenID with your BOT tokenID replace the yourChannelChatID with your channel chart id.Please note that the channel id is not negative id.
the code will look like this
import request
url = "https://api.telegram.org/botXyz:wwwwDFSJSJSAX/sendMessage?chat_id=#telechanneltesting&text=message"
requests.post(url)
Here the "message" as in the URL it will be posted in telegram channel.
How to get channel id ?
Go to https://web.telegram.org/#/im in browser ,after login now search your "channel".Now in the browser address bar you will one link like https://web.telegram.org/#/im?p=#llliopppppsssssr
p=#llliopppppsssssr after the equal symbol what comes is channel chat ID.
to send message to the telegram "group" without any user intervention , you require a telegram bot. create one using telegram bot-father. take a look at this link. also note the token while creating the bot. This method will work only for telegram Group .Telegram channel another method to be followed which MarxBabu answered below in his answers post.
import requests
# telegram url
url = "https://api.telegram.org/bot<Token>"
def send_mess(text):
params = {'chat_id':-xxxxxxx, 'text': text}
response = requests.post(url + 'sendMessage', data=params)
return response
send_mess(result_you_want_to_send)
to get the chat_id follow the steps mentioned here. note: group chat id's always starts with '-' . e.g. of group chat_id -356163873. A token and chat_id is only what you require to send message to telegram group.
for sending group emails you have to search more as i do not know much
I have it running on a Raspberry pi. You must look for botfather to get your telegram token.
import telepot
from telepot.loop import MessageLoop
telegram_token = 'xxxx:xxxxxx'
user = 4444444
bot = telepot.Bot(telegram_token)
bot.sendMessage(user, 'Hey!')
For configuring gmail I don't have something here right now...
You can send emails in python through SMTP or using the Mailgun Api
I personally prefer using Mailgun as it is easier to setup and sending an email is as easy as sending a post request to mailgun.
You can get the free version of the api and add your group email id to the sandbox (to avoid spam) and then use requests to post an email with the given api token

Changing Slack bot name / image using Slash Commands?

I'm setting up a slackbot with slash commands and according to API you're able to pass username to set the username of a bot. When I try it, the name does not update. Does this not work with slash commands in the same way that it does with bots?
from bottle import run, post, request
#post('/hello')
def hello():
mydict = request.forms.get('text')
response = {
"username": "test",
"response_type": "in_channel",
"text": "Greetings!"
}
return response
matt [11:00 AM]
/hello
MyBot APP [11:00 AM]
Greetings!
Correct. You can not change the name of the app that appears on Slack when you respond to a request like that. It will always show the name you configured in the app settings.
However, there is a workaround for your use case: Instead of replying to the received request directly you can send a message into the channel / to the user with chat.postMessage, which will allow you to specify a custom username and icon.

write a script to send files to box application by automatic developer token generation

Requirement: I need a script that sends files to box.
So far, i managed to send the files to box by following https://docs.box.com/docs/getting-started-box-integration . I am using box-python-sdk to acheive this, but i have to use this developer token which is active only for 60 min for authenticaton. I have to generate a developer token everyday to run this script.
oauth = OAuth2(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
access_token=DEVELOPER_TOKEN,
)
client = Client(oauth)
I cannot set the redirect URI as am not running an application on a server, am just running this script using "python box.py ". Is there a way to refresh the developer token in my script?
This can be achieved using JWT authentication, refer https://github.com/box/box-python-sdk.

GCM: cannot send message from python script

I'm following this tutorial to send GCM message from a server via a python script.
I've downloaded the GcmDemo that can successfully send messages to itself using my project num, server API key and reg id.
But no way to do it from python.
I've registered the client:
06-13 09:47:45.682 2272-32480/com.google.android.gcm.demo I/GcmDemo﹕ registration succeeded.
senderId: 11xxxxxxx07
token: fxq...xxxx..r5
My server API key:
The python script on the server:
from gcm import *
gcm = GCM("AIza...xxx...XQ")
data = {'ciao ciao': 'test message', 'param2': 'value2'}
reg_id = 'fxq...xxxx..r5'
gcm.plaintext_request(registration_id=reg_id, data=data)
No error messages but no messages received.
Is there a way to log and see where the problem is ?
The client doesn't react (log on Android Studio set to verbose, no log at all).
Any idea ?
The google project console just shows logs till 30 May.
I'm not using Python anymore as I switched to PHP but I had the same problem there and solved adding the icon field to the "data". So adding something like:
data = {'icon' : '#drawable/myAwesomeIcon', ...}
should solve the problem.
For me it was not working when i put dry_Run =True, but worked on removing drun_Run.dry_Run=True was giving me fake_message_id.

Categories

Resources